测试工程连接https://files.cnblogs.com/deadshot123/Log4NetTest.rar
配置日志到数据库可参照http://logging.apache.org/log4net/release/config-examples.html
一、添加一个web的站点上的log.xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net" />
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="MyAppender"/>
</root>
<logger name="testApp.Logging">
<level value="DEBUG" />
</logger>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n" />
<param name="Footer" value="[Footer]\r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
<!--2006-10-26 13:15:05,356 [2664] DEBUG defaultapp [] - u click the button 1.-->
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>
<appender name="MyAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Logs//" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="100" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyy-MM-dd".txt"" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="Log//" />
<!--指明绝对路径也可以-->
<!--<param name="File" value="d:\\Log\\"-->
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="100" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyy-MM-dd".htm"" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="<HR COLOR=red>%n异常时间:%d [%t] <BR>%n异常级别:%-5p
<BR>%n异 常 类:%c [%x] <BR>%n%m <BR>%n <HR Size=1>" />
</layout>
</appender>
</log4net>
</configuration>
<!--
<appender-ref ref="LogFileAppender" />
<appender-ref ref="ConsoleAppender" />
-->
二、在类库的assembly.config中添加
[assembly:log4net.Config.DOMConfigurator(ConfigFile="log.xml",Watch=true)]
类库中定义一个日志类
using System;
namespace Common
{
/// <summary>
/// Logger 的摘要说明。日志操作对象
/// </summary>
public class Logger
{
/// <summary>
/// 隐藏的构造函数,防止被实例化
/// </summary>
private Logger()
{
}
/// <summary>
/// 静态的全局日志变量
/// </summary>
public static readonly log4net.ILog ilog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);//这个反射在static下没有意义
/// <summary>
/// log4net的debug级别下的日志
/// </summary>
/// <param name="msg">被记录对象</param>
public static void DebugLog(object msg)
{
// string caller=mb.Name;
// for(int i=0;i< st.FrameCount;i++)
// {
// System.Diagnostics.StackFrame sf=st.GetFrame(i);
// System.Diagnostics.Debug.WriteLine(String.Format("File name:{0}, Line:{1}, Column:{2},MethodName:{3}", sf.GetFileName(), sf.GetFileLineNumber(), sf.GetFileColumnNumber(), sf.GetMethod().Name));
// }
if (ilog.IsDebugEnabled)//提高效率
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
System.Reflection.MethodBase mb=st.GetFrame(1).GetMethod();
ilog.Debug("logger name is:"+ilog.Logger.Name+"\r\nCaller class is:"+mb.DeclaringType+"\r\nCaller Method is:"+mb.Name+"\r\nmessage is:"+msg.ToString());
//ilog.Debug(msg);
//ilog.Debug(msg);
}
}
/// <summary>
/// log4net的Error级别下的日志
/// </summary>
/// <param name="msg">被记录对象</param>
public static void ErrorLog(object msg)
//由于System.Web.UI.TemplateControl.Error()的存在 所以加了Log而不写New。
{
if (ilog.IsErrorEnabled)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
System.Reflection.MethodBase mb=st.GetFrame(1).GetMethod();
ilog.Error("logger name is:"+ilog.Logger.Name+"\r\nCaller class is:"+mb.DeclaringType+"\r\nCaller Method is:"+mb.Name+"\r\nmessage is:"+msg.ToString());
}
}
/// <summary>
/// log4net的Warn级别下的日志
/// </summary>
/// <param name="msg">被记录对象</param>
public static void WarnLog(object msg)
{
if (ilog.IsWarnEnabled)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
System.Reflection.MethodBase mb=st.GetFrame(1).GetMethod();
ilog.Warn("logger name is:"+ilog.Logger.Name+"\r\nCaller class is:"+mb.DeclaringType+"\r\nCaller Method is:"+mb.Name+"\r\nmessage is:"+msg.ToString());
}
}
/// <summary>
/// log4net的Info级别下的日志
/// </summary>
/// <param name="msg">被记录对象</param>
public static void InfoLog(object msg)
{
if (ilog.IsInfoEnabled)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
System.Reflection.MethodBase mb=st.GetFrame(1).GetMethod();
ilog.Info("logger name is:"+ilog.Logger.Name+"\r\nCaller class is:"+mb.DeclaringType+"\r\nCaller Method is:"+mb.Name+"\r\nmessage is:"+msg.ToString());
}
}
/// <summary>
/// log4net的Fatal级别下的日志
/// </summary>
/// <param name="msg">被记录对象</param>
public static void FatalLog(object msg)
{
if (ilog.IsFatalEnabled)
{
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
System.Reflection.MethodBase mb=st.GetFrame(1).GetMethod();
ilog.Fatal("logger name is:"+ilog.Logger.Name+"\r\nCaller class is:"+mb.DeclaringType+"\r\nCaller Method is:"+mb.Name+"\r\nmessage is:"+msg.ToString());
}
}
}
}
三、在web程序中使用
web站点添加引用类库Common.dll
Common.Logger.DebugLog(ip+"Called HelloWorld");进行调用
参考:http://www.cnblogs.com/monthkey/archive/2005/04/30/148278.html
http://www.cnblogs.com/basilwang/archive/2006/08/03/467243.html
http://www.cnblogs.com/wangchunlan2004/archive/2006/08/10/473400.html
生成文件的格式有三种
1。按日期
2。按文件大小
3。同时按日期和文件大小
关键节点
<param name="MaxFileSize" value="10240" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMddHH".htm"" />
<param name="RollingStyle" value="Composite" />
log4net.Layout.PatternLayout中的转换模式(ConversionPattern)
%m(message):输出的日志消息,如ILog.Debug(…)输出的一条消息
%n(new line):换行
%d(datetime):输出当前语句运行的时刻
%r(run time):输出程序从运行到执行到当前语句时消耗的毫秒数
%t(thread id):当前语句所在的线程ID
%p(priority): 日志的当前优先级别,即DEBUG、INFO、WARN…等
%c(class):当前日志对象的名称,例如:
模式字符串为:%
代码为:
ILog log=LogManager.GetLogger(“Exam.Log”);
log.Debug(“Hello”);
则输出为下面的形式:
Exam.Log - Hello
%L:输出语句所在的行号
%F:输出语句所在的文件名
%-数字:表示该项的最小长度,如果不够,则用空格填充
例如,转换模式为%r [%t]%-5p %c - %m%n 的 PatternLayout 将生成类似于以下内容的输出:
176 [main] INFO org.foo.Bar - Located nearest gas station.