Log4Net不会从类库记录日志 [英] Log4Net does not log from a class library
问题描述
我正在Windows 7中使用C#在.NET Framework 4.0上工作,并尝试从类库中登录,但是它不起作用.我正在运行我的应用程序而没有错误,但是我的日志文件也没有任何反应,控制台也没有任何反应.
I am working on .NET Framework 4.0 using C# in Windows 7, and trying to log from a class library but it's not working. I'm running my application without errors, but also nothing happens to my log file, neither to my console.
所以,这是我的代码:
这是我的App.config
文件:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<add key="log4net.config" value="config.log4net"/>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra Info: %property{testProperty}%newline%exception"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG"/>
<levelMax value="FATAL"/>
</filter>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="MyLogFile.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="debug"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="error"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="RollingFileAppender"/>
</root>
<logger name="MyApplication">
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="RollingFileAppender"/>
</logger>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
这就是我放入AssemblyInfo.cs
的内容:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
这是我要记录的文件的内容:
This what is at the file i'm trying to log:
private static readonly ILog log = LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Debug("Testing");
当我运行程序时,什么也没发生.有人知道为什么吗?
When i run my program nothing happens. Someone knows why?
推荐答案
类似于此答案,都是该应用. config,log4net配置和AssemblyInfo.cs配置器需要放置在主机应用程序中.
Similar to this answer, both the app.config, log4net configuration and the AssemblyInfo.cs configurator needs to be placed in the host application.
让我们说我有一个项目Console
作为将要运行的控制台项目,并且有Library
作为一个库项目. Console
将需要具有带有上述log4net配置的app.config文件,而AssemblyInfo.cs将需要在其中包含XmlConfigurator代码. Library
不需要任何一个,并且可以通过使用LogManager
调用来工作.
Lets say I have project Console
as a console project that I will run, and Library
as a Library project. Console
will need to have an app.config file with the above log4net configuration, and the AssemblyInfo.cs will need the XmlConfigurator code inside of it. Library
does not need either of these, and will work by using the LogManager
call.
Console > This is the project that you will run.
Properties
AssemblyInfo.cs > Place [assembly: log4net.Config.XmlConfigurator(Watch = true)] here.
app.config > Place log4net XML configuration here.
Program.cs > Start of application.
Library
Properties
AssemblyInfo.cs > Leave this file alone.
Foo.cs > Create the private readonly property.
SomeMethod() > log.Debug("Test");
这篇关于Log4Net不会从类库记录日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!