• log4net 配置使用


    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
      </configSections>
      <appSettings>
      </appSettings>
      <log4net>
        <!--定义输出到文件中-->
        <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
          <!--定义文件存放位置-->
          <file value="./log/log.txt" />
          <appendToFile value="true" />
          <rollingStyle value="Date" />
          <datePattern value="yyyyMMdd-HH:mm:ss" />
          <layout type="log4net.Layout.PatternLayout">
            <!--每条日志末尾的文字说明-->
            <footer value="by lcz" />
            <!--输出格式-->
            <!--样例:2008-03-26 13:42:32,111 [10] INFO  Log4NetDemo.MainClass [(null)] - info-->
            <conversionPattern value="记录时间:%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger property:[%property{NDC}] - 错误描述:%message%newline" />
          </layout>
        </appender>
        <!--定义输出到控制台命令行中-->
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
          </layout>
        </appender>
        <!--定义输出到windows事件中-->
        <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
          </layout>
        </appender>
        <!--定义输出到数据库中,这里举例输出到Access数据库中,数据库为C盘的log4net.mdb-->
        <appender name="AdoNetAppender_Access" type="log4net.Appender.AdoNetAppender">
          <connectionString value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:log4net.mdb" />
          <commandText value="INSERT INTO LogDetails ([LogDate],[Thread],[Level],[Logger],[Message]) VALUES (@logDate, @thread, @logLevel, @logger,@message)" />
          <!--定义各个参数-->
          <parameter>
            <parameterName value="@logDate" />
            <dbType value="String" />
            <size value="240" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%date" />
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@thread" />
            <dbType value="String" />
            <size value="240" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%thread" />
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@logLevel" />
            <dbType value="String" />
            <size value="240" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%level" />
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@logger" />
            <dbType value="String" />
            <size value="240" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%logger" />
            </layout>
          </parameter>
          <parameter>
            <parameterName value="@message" />
            <dbType value="String" />
            <size value="240" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%message" />
            </layout>
          </parameter>
        </appender>
        <!--定义日志的输出媒介,下面定义日志以四种方式输出。也可以下面的按照一种类型或其他类型输出。-->
        <root>
          <!--文件形式记录日志-->
          <appender-ref ref="LogFileAppender" />
          <!--控制台控制显示日志-->
          <appender-ref ref="ConsoleAppender" />
          <!--Windows事件日志-->
          <appender-ref ref="EventLogAppender" />
          <!-- 如果不启用相应的日志记录,可以通过这种方式注释掉
          <appender-ref ref="AdoNetAppender_Access" />
          -->
        </root>

      </log4net>
    </configuration>

    调用代码1、

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using log4net;
    [assembly: log4net.Config.XmlConfigurator(Watch = true)]
    namespace yichangDEMO {
     class LogHelper {

      public static readonly ILog loginfo = log4net.LogManager.GetLogger("EventLogAppender"); //选择输出到windows事件中
      
      public static void SetConfig() {
       log4net.Config.XmlConfigurator.Configure();
      }

      public static void SetConfig(FileInfo configFile) {
       log4net.Config.XmlConfigurator.Configure(configFile);
      }

      public static void WriteLog(string info) {
       if (loginfo.IsInfoEnabled) {
        loginfo.Info(info);
       }
      }

      public static void WriteLog(string info, Exception se) {


       if (loginfo.IsErrorEnabled) {
        loginfo.Error(info, se);
       }
      }
     }
    }

    调用代码2、

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;

    namespace yichangDEMO {

     static class Program {
      /// <summary>
      /// 应用程序的主入口点。
      /// </summary>
      [STAThread]
      static void Main() {
       Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
       AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
       Application.EnableVisualStyles();
       Application.SetCompatibleTextRenderingDefault(false);
       Application.Run(new Form1());
      }
      static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) {
       Exception ex = e.Exception;
       LogHelper.WriteLog("错误:", ex);
      }
      static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
       Exception ex = (Exception)e.ExceptionObject;
       LogHelper.WriteLog("错误:", ex);
      }
     }
    }

    这里同上偏随笔结合使用

    已验证

    原文链接http://blog.csdn.net/zhoufoxcn/article/details/2220533

  • 相关阅读:
    sqlserver内存释放
    Windows任务管理器中内存使用、虚拟内存区别及与页面文件的关系
    GetMessage
    String.Format(string, arg0)中sring格式
    C#基础--之数据类型
    C# Socket
    C# 对象 序列化 XML
    C# Monitoring-network
    Nginx 网址
    WinSCP 连接 Ubuntu 拒绝的问题
  • 原文地址:https://www.cnblogs.com/lczblog/p/3519332.html
Copyright © 2020-2023  润新知