[英]log multiple assemblies using log4net
After lots of issues, finally I managed to configure log4net for my window service. 经过很多问题之后,终于我为窗口服务配置了log4net。
I am pretty new to it and today I started configuring it. 我对它很陌生,今天开始配置它。 I have these below doubts.
我对此有以下怀疑。
1) I need to use that into multiple assemblies. 1) 我需要将其用于多个程序集中。 Say I have an assembly 'A' which is added as reference in assembly 'B' which is mine main assembly where I have reference of log4net.I need to log both assembly 'A' and 'B'.
假设我有一个装配'A',它被添加为装配'B'的参考,装配'B'是我的主装配,我引用了log4net。我需要同时记录装配'A'和'B'。
2) My application is multi-threaded and use lots of thread. 2)我的应用程序是多线程的,并且使用很多线程。 So is log4net is thread safe?
那么log4net是线程安全的吗?
3) I am using this below config in my app.config. 3)我在我的app.config中的config下面使用它。 I am not much aware what's the use of it.
我不太了解它的用途。 But I don't want to use unnecessary parameters.
但是我不想使用不必要的参数。
<log4net>
<root>
<level value="DEBUG"/>
<appender-ref ref="LogFileAppender"/>
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="C:\logs\log.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="100KB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
</layout>
</appender>
</log4net>
Use <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
under appender section. 在附加器部分下使用
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
。 I twill increase the logging performance. 我会提高日志记录性能。
log4net is threadsafe. log4net是线程安全的。
<maximumFileSize value="10MB" /> //For 100 Kb configuration there will be lot of files.
<datePattern value="_yyyyMMdd" />
//It will hint the logger to create a new file per date. <datePattern value="_yyyyMMdd" />
//它将提示记录器按日期创建一个新文件。
Create a static class for Logger and call the static function from every assembly where you want to use. 为Logger创建一个静态类,并从要使用的每个程序集中调用该静态函数。
Sample Class for logging: 用于记录的样本类:
public static class Logger
{
static Logger()
{
XmlConfigurator.Configure();
}
public static void Log()
{
string methodName = new System.Diagnostics.StackFrame(1, true).GetMethod().Name;
string moduleName = new System.Diagnostics.StackFrame(1, true).GetMethod().ReflectedType.FullName;
var appLog = LogManager.GetLogger(loggername);
appLog.Error(...);
}
}
ILog
as usual in assembly B. ILog
。 Configure()
once in your application's lifetime. Configure()
即可。 <root>
<level value="DEBUG"/>
<appender-ref ref="LogFileAppender"/>
</root>
It will log only in debug mode, so make following entry in config: 它只会以调试模式记录日志,因此请在config中进行以下输入:
<root>
<level value="ALL"/>
<appender-ref ref="LogFileAppender"/>
</root>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.