欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.NET 日志工具 log4net使用

程序员文章站 2022-03-14 08:46:24
1.NuGet安装log4net. 2.修改配置文件 3.AssemblyInfo.cs中加入代码 // 日志组件配置 4.公共类封装方法调用log4net 1 using System; 2 using System.Collections.Generic; 3 using System.Conf ......

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

.NET 日志工具 log4net使用
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Configuration;
 4 using System.Linq;
 5 using System.Web;
 6 
 7 namespace TestLog4Net
 8 {
 9     public class MyLog4NetInfo
10     {
11         private static readonly log4net.ILog log = log4net.LogManager.GetLogger("WebLogger");
12 
13         public MyLog4NetInfo()
14         {
15         }
16 
17         private static void SetConfig()
18         {
19             object o = ConfigurationManager.GetSection("log4net");
20             log4net.Config.XmlConfigurator.Configure(o as System.Xml.XmlElement);
21         }
22 
23         public static void LogInfo(string Message)
24         {
25             if (!log.IsInfoEnabled)
26                 SetConfig();
27             log.Info(Message);
28         }
29 
30         public static void LogInfo(string Message,Exception ex)
31         {
32             if (!log.IsInfoEnabled)
33                 SetConfig();
34             log.Info(Message, ex);
35         }
36 
37         public static void ErrorInfo(string Message)
38         {
39             if (!log.IsInfoEnabled)
40                 SetConfig();
41             log.Error(Message);
42         }
43 
44         public static void DebugInfo(string Message)
45         {
46             if (!log.IsInfoEnabled)
47                 SetConfig();
48             log.Debug(Message);
49         }
50     }
51 }
View Code

5.测试页面Log4NetTest.aspx

.NET 日志工具 log4net使用
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Log4NetTest.aspx.cs" Inherits="TestLog4Net.Log4NetTest" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12         <asp:Button ID="button1" runat="server" Text="Button" OnClick="button1_Click" />
13     </form>
14 </body>
15 </html>
View Code

Log4NetTest.aspx.cs

.NET 日志工具 log4net使用
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 namespace TestLog4Net
 9 {
10     public partial class Log4NetTest : System.Web.UI.Page
11     {
12         protected void Page_Load(object sender, EventArgs e)
13         {
14 
15         }
16 
17         protected void button1_Click(object sender, EventArgs e)
18         {
19             MyLog4NetInfo.LogInfo("错误日志test");
20             MyLog4NetInfo.LogInfo("错误日志test");
21             MyLog4NetInfo.LogInfo("错误日志test");
22         }
23     }
24 }
View Code