1 修改webconfig文件
[xhtml] view plain copy
<?xml version="1.0"?>
<configuration>
<configSections>
<!-- log4net的定义 -->
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<logger name="Service">
<level value="ALL" />
<appender-ref ref="rollingFileService" />
</logger>
<appender name="rollingFileService" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" type="" value="./log/Service/log" />
<param name="AppendToFile" value="true" />
<param name="Encoding" value="Unicode" />
<param name="RollingStyle" value="composite" />
<param name="maximumFileSize" value="1MB" />
<param name="DatePattern" value="_yyyy-MM-dd.TXT" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value="◇" />
<param name="Footer" value="◆" />
</layout>
</appender>
<logger name="Mobile">
<level value="ALL" />
<appender-ref ref="rollingFileMobile" />
</logger>
<appender name="rollingFileMobile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" type="" value="./log/Mobile/log" />
<param name="AppendToFile" value="true" />
<param name="Encoding" value="Unicode" />
<param name="RollingStyle" value="composite" />
<param name="maximumFileSize" value="1MB" />
<param name="DatePattern" value="_yyyy-MM-dd.TXT" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value="◇" />
<param name="Footer" value="◆" />
</layout>
</appender>
</log4net>
<appSettings>
2.添加global.asax文件
[c-sharp] view plain copy
protected void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
}
3. Assembleyinfo文件中增加
[c-sharp] view plain copy
[assembly: log4net.Config.XmlConfigurator()]
4. 程序中 代码如下
[c-sharp] view plain copy
protected log4net.ILog Entrylog; = log4net.LogManager.GetLogger("Mobile");
///
/// 记录一个错误信息
///
/// 信息内容
/// 异常对象
public void Error(string message,Exception exception)
{
Entrylog.Error(message,exception);
}
///
/// 记录一个错误信息
///
/// 信息内容
public void Error(string message)
{
Entrylog.Warn(message);
}
///
/// 记录一个警告信息
///
/// 信息内容
public void Warning(string message)
{
Entrylog.Warn(message);
}
///
/// 记录一个程序致命性错误
///
/// 信息内容
/// 异常对象
public void Fatal(string message,Exception exception)
{
Entrylog.Fatal(message,exception);
}
///
/// 记录一个程序致命性错误
///
/// 信息内容
public void Fatal(string message)
{
Entrylog.Fatal(message);
}
///
/// 记录信息
///
/// 信息内容
/// 异常对象
public void Info(string message,Exception exception)
{
Entrylog.Info(message,exception);
}
///
/// 记录信息
///
/// 信息内容
public void Info(string message)
{
Entrylog.Info(message);
}
///
/// 记录调试信息
///
/// 信息内容
/// 异常对象
public void Debug(string message,Exception exception)
{
Entrylog.Debug(message,exception);
}
///
/// 记录调试信息
///
/// 信息内容
public void Debug(string message)
{
Entrylog.Debug(message);
}
---------------------
作者:moonshineidolon
来源:CSDN
原文:https://blog.csdn.net/moonshineidolon/article/details/4103970
版权声明:本文为博主原创文章,转载请附上博文链接!