Seb*_*ler 17 .net logging log4net
我尝试配置log4net以将所有内容记录到控制台输出.我有一个名为的配置文件Log4Net.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我有我的主要课程(只是一个测试案例)
namespace TestLog4Net {
class Program {
private static readonly ILog log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args) {
log.Info("Info");
log.Error("Error");
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我把这些线添加到了 AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(
ConfigFile = "Log4Net.config", Watch = true)]
Run Code Online (Sandbox Code Playgroud)
但现在没有记录,有人可以解释一下吗?
NiT*_*ded 18
通过代码尝试此自定义配置.为我工作并在多个应用程序中测试..
string logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "\\Config\\Log4Net.config");
FileInfo finfo = new FileInfo(logFilePath);
log4net.Config.XmlConfigurator.ConfigureAndWatch(finfo);
Run Code Online (Sandbox Code Playgroud)
我知道这是一个非常晚的回复.但对某些权利会有用.
我相信您在帖子中提到的文件是App.config文件的内容。App.config是各种 C# 项目模板(如 WinForm 应用程序、控制台应用程序等)的默认配置文件。因此,您应该考虑将其命名为App.config来代替Log4Net.config。
为了有一个完整的理解,让我们看看配置 log4net 的三种可能的方法:
如果我们想使用应用程序的App.config文件来保存 的设置log4net,那么我们必须在AssemlyInfo.cs文件中将下面提到的声明添加到以下行:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Run Code Online (Sandbox Code Playgroud)
App.config将保存 log4net 设置,如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>
Run Code Online (Sandbox Code Playgroud)
在某些情况下,我们可能希望使用专用文件来保存log4net. 我们可以将其命名为log4net.config或log4net.xml。在创建这样的专用配置文件时,我们需要确保它log4net是配置文件的根标签(请参阅文件片段):
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="ConsoleAppender" />
</root>
</log4net>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我们需要在AssemblyInfo.cs文件中进行以下声明。OP 的问题中也提到了相同的声明:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
Run Code Online (Sandbox Code Playgroud)
现在是最后的用例。假设我们有一个自己的自定义 xml 文件,例如myCustomSettings.xml,其中包含log4net设置。在这种情况下,log4net 标签存在于自定义配置文件中,但它不是该文件的根标签(请参阅文件片段)
<MyCustomSetting>
<mySection1></mySection1>
<mySection2></mySection2>
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="file" />
<appender-ref ref="console" />
</root>
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level - %message%newline" />
</layout>
<threshold value="Info" />
</appender>
<appender name="file" type="log4net.Appender.RollingFileAppender">
<file value="${PROGRAMDATA}\MyProduct\myLogs.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<threshold value="Debug" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
</log4net>
</MyCustomSetting>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我们必须在运行时通过 C# 代码配置log4net (请参阅代码片段)。
using System.Xml;
var configFilePath = "myCustomSettings.xml";
var log4netConfig = new XmlDocument();
log4netConfig.Load(File.OpenRead(configFilePath));
var repo = LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy));
var log4NetXml = log4netConfig["MyCustomSetting"]["log4net"];
log4net.Config.XmlConfigurator.Configure(repo, log4NetXml);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我们不需要AssemblyInfo.cs文件中的声明性配置。因此,请确保您已将其从那里删除:
//TODO: Remove it
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36487 次 |
| 最近记录: |