1.NuGet安装log4net.
2.修改配置文件
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<!-- Set root logger level to ERROR and its appenders -->
<root>
<level value="ALL"/>
<appender-ref ref="SysAppender"/>
</root> <!-- Print only messages of level DEBUG or above in the packages -->
<logger name="WebLogger">
<level value="ALL"/>
</logger>
<appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Logger/" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value=""Logs_"yyyyMMdd".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="
----------------------header--------------------------
" />
<param name="Footer" value="
----------------------footer--------------------------
" />
</layout>
</appender>
</log4net>
</configuration>
3.AssemblyInfo.cs中加入代码
// 日志组件配置
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", ConfigFileExtension = "config", Watch = true)]
4.公共类封装方法调用log4net
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web; namespace TestLog4Net
{
public class MyLog4NetInfo
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger("WebLogger"); public MyLog4NetInfo()
{
} 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);
}
}
}
5.测试页面Log4NetTest.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Log4NetTest.aspx.cs" Inherits="TestLog4Net.Log4NetTest" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="button1" runat="server" Text="Button" OnClick="button1_Click" />
</form>
</body>
</html>
Log4NetTest.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace TestLog4Net
{
public partial class Log4NetTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void button1_Click(object sender, EventArgs e)
{
MyLog4NetInfo.LogInfo("错误日志test");
MyLog4NetInfo.LogInfo("错误日志test");
MyLog4NetInfo.LogInfo("错误日志test");
}
}
}