刚开始接触asp.net,关于日志记录怎么能少,因此简单记录一下log4net的配置和使用,以防以后忘记。
首先引入log4net.dll,关于这个文件自己百度下载下。
然后配置一下有关配置,在web.config中加入在configuration节点下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<configSections>
<section name= "log4net" type= "log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name= "rollingFile" type= "log4net.Appender.RollingFileAppender,log4net" >
<param name= "File" value= "log/" />
<param name= "AppendToFile" value= "true" />
<param name= "RollingStyle" value= "Date" />
<param name= "DatePattern" value= "yyyy-MM-dd" .log "" />
<param name= "StaticLogFileName" value= "false" />
<layout type= "log4net.Layout.PatternLayout" >
<param name= "ConversionPattern" value= "%n%r %p[%d %c] %m%n" />
</layout>
</appender>
<root>
<level value= "ALL" />
<appender- ref ref = "rollingFile" />
</root>
<logger name= "WebLogger" >
<level value= "ALL" />
<appender- ref ref = "rollingFile" />
</logger>
</log4net>
|
然后在Global.asax的Application_Start方法中加入
1
|
log4net.Config.XmlConfigurator.Configure();
|
最后写一个日志输出类,方便调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
public class MyLog
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger( "WebLogger" );
public MyLog()
{
}
private static void SetConfig()
{
object o = ConfigurationManager.GetSection( "log4net" );
log4net.Config.XmlConfigurator.Configure(o as System.Xml.XmlElement);
}
public static void LogInfo( string Message)
{
if (!log.IsInfoEnabled)
SetConfig();
log.Info(Message);
}
public static void LogInfo( string Message, Exception ex)
{
if (!log.IsInfoEnabled)
SetConfig();
log.Info(Message, ex);
}
public static void ErrorInfo( string Message)
{
if (!log.IsInfoEnabled)
SetConfig();
log.Error(Message);
}
public static void DebugInfo( string Message)
{
if (!log.IsInfoEnabled)
SetConfig();
log.Debug(Message);
}
}
|
然后就可以在各个代码中使用日志输出啦!
关于log4net的配置暂时还没有仔细看,等有空了再继续研究。