Log4Net 不记录或错误
我一直在尝试在我的 ASP.NET Web 应用程序中获取 log4net 日志记录,但没有成功或任何明显的错误。 我尝试使用具有以下配置的 ADONetAppender 附加程序:
<log4net>
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="server=" />
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception],[Context]) VALUES
(@log_date, @thread, @log_level, @logger, @message, @exception, @context)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="32" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%t" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="512" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%p" />
</layout>
</parameter>
<parameter>
<parameterName value="@context" />
<dbType value="String" />
<size value="512" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%x" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="512" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%c" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ADONetAppender" />
</root>
在我的 global.asax Application_Start 中,我调用
log4net.Config.XmlConfigurator.Configure();
Then 来尝试记录:
protected static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected void Page_Load(object sender, EventArgs e)
{
log.Info("did I log yet?");
}
据我所知,所有这些都没有执行任何操作。
I have been attempting to get log4net logging in my asp.net web application with no success or any noticable errors. I am attempting to use the ADONetAppender appender with the following config:
<log4net>
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="server=" />
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception],[Context]) VALUES
(@log_date, @thread, @log_level, @logger, @message, @exception, @context)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="32" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%t" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="512" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%p" />
</layout>
</parameter>
<parameter>
<parameterName value="@context" />
<dbType value="String" />
<size value="512" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%x" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="512" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%c" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ADONetAppender" />
</root>
In my global.asax Application_Start I call
log4net.Config.XmlConfigurator.Configure();
Then to attempt to log:
protected static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected void Page_Load(object sender, EventArgs e)
{
log.Info("did I log yet?");
}
All of this does nothing as far as I can tell.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
log4Net 的
内部调试
是@Pratik 建议的方法。 要将详细信息写入文件,请执行以下操作:即使您为 log4net 使用不同的配置文件,也请在
web.config
中添加以下内容。参考
Internal debugging
for log4Net is the way to go as suggested by @Pratik. To write the details into a file do the following:Add the following in
web.config
even if you are using a different config file for log4net.Reference
您是否在 Configuration->Configsections 下添加了以下部分
虽然可以将 log4net 配置设置添加到项目的 app.config 或 web.config 文件中,但最好将它们放在单独的配置文件中。 除了可维护性方面的明显好处之外,它还有一个额外的好处,即 log4net 可以在配置文件上放置一个 FileSystemWatcher 对象来监视它何时更改并动态更新其设置。
要使用单独的配置文件,请将名为
Log4Net.config
的文件添加到您的项目中,并将以下属性添加到AssemblyInfo.cs
文件中:注意:对于 Web 应用程序,假设 Log4Net.config 位于 Web 根目录中。 确保 log4net.config 文件被标记为“复制到输出”-> 属性中的“始终复制”。
来自此处。
Have you added added the following section under under Configuration->Configsections
Although it is possible to add your log4net configuration settings to your project’s app.config or web.config file, it is preferable to place them in a separate configuration file. Aside from the obvious benefit of maintainability, it has the added benefit that log4net can place a
FileSystemWatcher
object on your config file to monitor when it changes and update its settings dynamically.To use a separate config file, add a file named
Log4Net.config
to your project and add the following attribute to yourAssemblyInfo.cs
file:Note: for web applications, this assumes Log4Net.config resides in the web root. Ensure the log4net.config file is marked as “Copy To Output” -> “Copy Always” in Properties.
From here.
您还可以设置以下应用程序设置以启用内部 log4net 调试信息
内部调试消息将写入控制台(如果有的话)或跟踪(例如 Visual Studio 中的调试窗口或 sysinternals 中的 dbgview)。 这将帮助您解决 log4net 问题。
you can also set the following appsettings to enable internal log4net debugging information
The internal debug messages are written to the console (if you have one) or the trace (such as the debug window in visual studio or dbgview from sysintenals). This will help you to troubleshoot log4net issues.