1.下载 或 在nuget安装 log4net
2. web.config (app.config)
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net" />
</configSections>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="C:\m1\eDASLog\%date{dd.MM.yyyy}.txt" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[[%date]] - %message %newline" />
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
</configuration>
为了每天一份log文件,做这个配置(文件节点类型为PatternString):
<file type="log4net.Util.PatternString" value="C:\m1\eDASLog\%date{dd.MM.yyyy}.txt" />
3. 应用配置,在Application_Start(对于web 应用)中configure():
log4net.Config.XmlConfigurator.Configure();
4.做一个简单的wrapper来使用:
public static class EdasLogger
{
private static log4net.ILog Log { get; set; }
static EdasLogger()
{
Log = log4net.LogManager.GetLogger(typeof(Logger));
}
public static void Error(object msg)
{
Log.Error(msg);
}
public static void Error(object msg, Exception ex)
{
Log.Error(msg, ex);
}
public static void Error(Exception ex)
{
Log.Error(ex.Message, ex);
}
public static void Info(object msg)
{
Log.Info(msg);
}
}