使用相同的 log4net 实例登录所有项目
use the same instance of log4net to log into all projects
我在 c# 解决方案中使用 Log4net,该解决方案在一个解决方案中包含 3 个项目。
我想使用这个记录器的同一个实例来登录一个文件。
这是 App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<appender name="TestAppender" type="log4net.Appender.RollingFileAppender" >
<file value="C:\log\MyTestAppender.log" />
<!--<file value="../../test/MyTestAppender.log" />-->
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<!--<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %type.%method - %message%n" />
</layout>
</appender>
<root>
<level value="All" />
<!-- If the following line is not included the log file will not be created even if log4net is configured with this file. -->
<appender-ref ref="TestAppender" />
</root>
</log4net>
</configuration>
我这样做是为了记录
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
我应该将这些信息放在哪里才能从所有项目中登录同一个日志文件。
非常感谢
你可以这样做,我的意思是说在静态 class 上创建并创建日志实例,然后在该静态 class 上调用日志方法,它将只使用一个日志实例。
public static class Utility
{
private static ILog log = LogManager.GetLogger("CommonLogger");
public static void LogMessage(string message)
{
Task.Factory.StartNew(() => log.Info(message));
}
}
或者您也可以为此
使用 Singleton
模式
public Class LogSingleTon
{
private static readonly Lazy<LogSingleTon> lazy =
new Lazy<LogSingleTon>(() => new LogSingleTon());
public static LogSingleTon Instance { get { return lazy.Value; } }
private ILog log ;
private LogSingleTon()
{
log = LogManager.GetLogger("CommonLogger")
}
public void LogMessage(string message)
{
Task.Factory.StartNew(() => log.Info(message));
}
}
您应该在静态构造函数中加载配置文件。
像这样
static LogHelper()
{
log4net.Config.XmlConfigurator.ConfigureAndWatch(
new System.IO.FileInfo(@"AppConfig\log4net.config"));
}
配置文件应该是这样的:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Register log4net -->
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<!--log configs -->
</log4net>
</configuration>
如果您使用依赖注入框架,您也可以将您的 Log4net 记录器注册为一个(一个)实例,并将其注入到您想要使用它的任何地方。
例如使用 Autofac:
var builder = new ContainerBuilder();
// Log4net
var logger = LogManager.GetLogger("[Whatever name your would like to use for your logger]");
builder.RegisterInstance(logger);
我在 c# 解决方案中使用 Log4net,该解决方案在一个解决方案中包含 3 个项目。 我想使用这个记录器的同一个实例来登录一个文件。 这是 App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<appender name="TestAppender" type="log4net.Appender.RollingFileAppender" >
<file value="C:\log\MyTestAppender.log" />
<!--<file value="../../test/MyTestAppender.log" />-->
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<!--<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %type.%method - %message%n" />
</layout>
</appender>
<root>
<level value="All" />
<!-- If the following line is not included the log file will not be created even if log4net is configured with this file. -->
<appender-ref ref="TestAppender" />
</root>
</log4net>
</configuration>
我这样做是为了记录
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
我应该将这些信息放在哪里才能从所有项目中登录同一个日志文件。 非常感谢
你可以这样做,我的意思是说在静态 class 上创建并创建日志实例,然后在该静态 class 上调用日志方法,它将只使用一个日志实例。
public static class Utility
{
private static ILog log = LogManager.GetLogger("CommonLogger");
public static void LogMessage(string message)
{
Task.Factory.StartNew(() => log.Info(message));
}
}
或者您也可以为此
使用Singleton
模式
public Class LogSingleTon
{
private static readonly Lazy<LogSingleTon> lazy =
new Lazy<LogSingleTon>(() => new LogSingleTon());
public static LogSingleTon Instance { get { return lazy.Value; } }
private ILog log ;
private LogSingleTon()
{
log = LogManager.GetLogger("CommonLogger")
}
public void LogMessage(string message)
{
Task.Factory.StartNew(() => log.Info(message));
}
}
您应该在静态构造函数中加载配置文件。
像这样
static LogHelper()
{
log4net.Config.XmlConfigurator.ConfigureAndWatch(
new System.IO.FileInfo(@"AppConfig\log4net.config"));
}
配置文件应该是这样的:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- Register log4net -->
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<!--log configs -->
</log4net>
</configuration>
如果您使用依赖注入框架,您也可以将您的 Log4net 记录器注册为一个(一个)实例,并将其注入到您想要使用它的任何地方。
例如使用 Autofac:
var builder = new ContainerBuilder();
// Log4net
var logger = LogManager.GetLogger("[Whatever name your would like to use for your logger]");
builder.RegisterInstance(logger);