我的bin目录中有以下文件Log4net.config:
<?xml version="1.0" encoding="utf-8" ?>
<log4net xmlns="urn:log4net">
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<param name="file" value="MyLogFile.log"/>
<param name="appendToFile" value="false"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date (%logger) [%5level] - %message" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date (%logger) [%5level] - %message" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
<appender-ref ref="ConsoleAppender"/>
</root>
<logger name="NHibernate" additivity="false">
<level value="WARN"/>
</logger>
</log4net>
以下代码在我的AssemblyInfo.cs文件中:
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyTitle("My Project")]
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4net.config", Watch = true)]
当我运行程序时,我得到以下log4net调试输出:
log4net: log4net assembly [log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821]. Loaded from [D:\Data\Projects\Active\Clients\MyProject\src\MyProject.Importer\bin\Debug\log4net.dll]. (.NET Runtime [4.0.30319.1] on Microsoft Windows NT 6.1.7600.0)
log4net: DefaultRepositorySelector: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository for assembly [MyCompany.Framework, Version=2.1.72.0, Culture=neutral, PublicKeyToken=null]
log4net: DefaultRepositorySelector: Assembly [MyCompany.Framework, Version=2.1.72.0, Culture=neutral, PublicKeyToken=null] Loaded From [D:\Data\Projects\Active\Clients\MyProject\src\MyProject.Importer\bin\Debug\MyCompany.Framework.dll]
log4net: DefaultRepositorySelector: Assembly [MyCompany.Framework, Version=2.1.72.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: DefaultRepositorySelector: Assembly [MyCompany.Framework, Version=2.1.72.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: DefaultRepositorySelector: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
The thread 'vshost.RunParkingWindow' (0xd30) has exited with code 0 (0x0).
The thread '<No Name>' (0x15d0) has exited with code 0 (0x0).
log4net: Hierarchy: Shutdown called on Hierarchy [log4net-default-repository]
Log4net加载,但似乎没有处理我的配置文件。当我在AssemblyInfo.cs中注释掉该属性并在程序初始化期间运行以下代码时,它按预期工作:
var log4netConfig = "Log4net.config";
var log4netInfo = new FileInfo(log4netConfig);
log4net.Config.XmlConfigurator.ConfigureAndWatch(log4netInfo);
我做错了什么?我想从AssemblyInfo.cs加载。
答案 0 :(得分:36)
我也遇到了这种提升log4net的方法。 documentation表示您必须在应用程序启动时尽早拨打电话
如果使用配置属性,则必须调用log4net以允许它读取属性。对LogManager.GetLogger的简单调用将导致读取和处理调用程序集上的属性。 因此,必须在应用程序启动期间尽早进行日志记录调用,当然在加载和调用任何外部程序集之前。
尝试将记录器放在启动应用程序的同一个类中(program.cs,app.xaml,等等)。例如
private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
对于踢,任何对日志的调用(即使是在追加过程中被过滤或评估的日志,它也应该强制log4net来评估你的存储库/层次结构)。
static Program()
{
Log.Debug("Application loaded.");
}
答案 1 :(得分:6)
最后我找到了简单的解决方案,你可以在那里得到帮助
启动功能中的Global.asax
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure();
}
在任何使用类级别日志记录的类
添加名称空间
using log4net;
在班级
添加此代码行private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
在任何动作调用中使用日志功能
log.Error("test error q111..");
构造
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net " />
</configSection>
<log4net debug="true">
<!--AdoNet appender is use for write log file into sql server-->
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=DESKTOP-NLH31FH; Initial Catalog=SmallBizDb;Integrated Security=true" providerName="System.Data.SqlClient" />
<commandText value="INSERT INTO [dbo].[Logs] ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@logdate,@thread, @loglevel, @logger, @message, @exception)" />
<parameter>
<parameterName value="@logdate" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@loglevel" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<!--Add appender which you want to use, You can add more then one appender . Like if you want save log both plain text or sql server ,Add both appender.-->
<root>
<level value="Debug" />
<!--<appender-ref ref="RollingLogFileAppender" />-->
<!--Enable this line if you want write log file into plain text file-->
<appender-ref ref="AdoNetAppender" />
<!--Enable this line if you want write log file into sql server-->
</root>
</log4net>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
它可能对所有人都有帮助并且易于使用。感谢
答案 2 :(得分:4)
我确实将log4net.Config.XmlConfigurator.Configure(new FileInfo(Server.MapPath("~/Web.config")));
保留在Application_Start()内的Global.asax.cs中......所以我不需要在所有地方都执行命令。
答案 3 :(得分:2)
我通过将RepositoryAttribute添加到有问题的程序集的AssemblyInfo.cs文件来修复此问题。
/usr/local/Cellar/python/2.7.9
答案 4 :(得分:0)
我正在使用Web.Config部分配置Logger并手动记录事件,从global.asax引导记录器
static ILog logger = LogManager.GetLogger(<LoggerName>);
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure();
}
尝试从global.asax
引导它答案 5 :(得分:0)
我正在将log4Net与Windows服务一起使用。我尝试了其他答案中提到的所有可能选项。
对我来说,该服务的另一个实例仍然有效,我不得不从任务管理器中删除它。此后,问题已解决。
答案 6 :(得分:-1)
var log4NetPath = Server.MapPath("~/log4net.config");
FileInfo fileInfo = new FileInfo(log4NetPath);
XmlConfigurator.ConfigureAndWatch(fileInfo);