C# Log4Net 不记录或错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1057464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Log4Net doesn't log or error
提问by Luke Lowrey
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:
我一直试图在我的 asp.net web 应用程序中获取 log4net 登录,但没有成功或任何明显的错误。我正在尝试使用具有以下配置的 ADONetAppender appender:
<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
在我的 global.asax Application_Start 我打电话
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.
据我所知,所有这些都没有任何作用。
采纳答案by Mitch Wheat
Have you added added the following section under under Configuration->Configsections
您是否在 Configuration->Configsections 下添加了以下部分
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
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.
尽管可以将 log4net 配置设置添加到项目的 app.config 或 web.config 文件中,但最好将它们放在单独的配置文件中。除了可维护性的明显好处之外,它还有一个额外的好处,即 log4net 可以FileSystemWatcher
在您的配置文件中放置一个对象,以监控它何时更改并动态更新其设置。
To use a separate config file, add a file named Log4Net.config
to your project and add the following attribute to your AssemblyInfo.cs
file:
要使用单独的配置文件,请Log4Net.config
向您的项目添加一个名为的文件,并将以下属性添加到您的AssemblyInfo.cs
文件中:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
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.
注意:对于 Web 应用程序,这假设 Log4Net.config 驻留在 Web 根目录中。确保 log4net.config 文件在属性中标记为“复制到输出”->“始终复制”。
From here.
从这里开始。
回答by softveda
you can also set the following appsettings to enable internal log4net debugging information
您还可以设置以下 appsettings 以启用内部 log4net 调试信息
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
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.
内部调试消息被写入控制台(如果有)或跟踪(例如 Visual Studio 中的调试窗口或来自 sysintenals 的 dbgview)。这将帮助您解决 log4net 问题。
回答by LCJ
Internal debugging
for log4Net is the way to go as suggested by @Pratik. To write the details into a file do the following:
Internal debugging
对于 log4Net 是@Pratik 建议的方法。要将详细信息写入文件,请执行以下操作:
Add the following in web.config
even if you are using a different config file for log4net.
web.config
即使您为 log4net 使用不同的配置文件,也要添加以下内容。
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\toolbox\SmartClient\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
Reference
参考