1. 程式人生 > >日誌記錄~log4.net

日誌記錄~log4.net

mtp 錯誤 one cti filepath ted tel str 外部

1. 添加Log4net引用

2. 添加配置文件

Log.config

技術分享
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <log4net>
    <logger name="Log4NetTest">
      <level value="ERROR"/>
      <appender-ref ref="Log4NetTestAppender" />
        <!--Log4NetTestAppender和下面進行對應-->
    </logger>

    <logger name="
RYJ.Crm.Quartz"> <level value="ERROR"/> <appender-ref ref="QuartzAppender" /> </logger> <logger name="RYJ.Crm.Services"> <level value="ERROR"/> <appender-ref ref="ServiceAppender" /> </logger> <logger name="
RYJ.Crm.ExternalService"> <level value="ERROR"/> <appender-ref ref="ExternalServiceAppender" /> </logger> <logger name="RYJ.Crm.HeartBeat"> <level value="DEBUG"/> <appender-ref ref="HeartBeatAppender" /> </logger> <logger name="
RYJ.Crm.System"> <level value="ERROR"/> <appender-ref ref="SystemAppender" /> </logger> <logger name="RYJ.Crm.UrlFilter"> <level value="DEBUG"/> <appender-ref ref="UrlFilterAppender" /> </logger> <logger name="NHibernate"> <level value="OFF"/> <appender-ref ref="NHibernateAppender" /> </logger> <logger name="RYJ.Return"> <level value="DEBUG"/> <appender-ref ref="ReturnAppender" /> </logger> <logger name="RYJ.Crm.Event"> <level value="DEBUG"/> <appender-ref ref="EventAppender" /> </logger> <logger name="RYJ.Crm"> <level value="ERROR"/> <appender-ref ref="EmailAppender" /> </logger> <!--返回值異常日誌記錄--> <appender name="Log4NetTestAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Logs/Log4NetTest/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" /> <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="&#13;&#10;----------------------header--------------------------&#13;&#10;" /> <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" /> </layout> </appender> <!--返回值異常日誌記錄--> <appender name="ReturnAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Logs/Return/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" /> <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="&#13;&#10;----------------------header--------------------------&#13;&#10;" /> <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" /> </layout> </appender> <!--事件異常日誌記錄--> <appender name="EventAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Logs/Event/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" /> <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="&#13;&#10;----------------------header--------------------------&#13;&#10;" /> <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" /> </layout> </appender> <!--系統異常日誌記錄--> <appender name="SystemAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Logs/System/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" /> <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="&#13;&#10;----------------------header--------------------------&#13;&#10;" /> <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" /> </layout> </appender> <!--調度日誌記錄--> <appender name="QuartzAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Logs/Quartz/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" /> <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="&#13;&#10;----------------------header--------------------------&#13;&#10;" /> <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" /> </layout> </appender> <!--Service日誌記錄--> <appender name="ServiceAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Logs/Service/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" /> <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="&#13;&#10;----------------------header--------------------------&#13;&#10;" /> <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" /> </layout> </appender> <!--調用外部服務日誌記錄--> <appender name="ExternalServiceAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Logs/ExternalService/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" /> <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="&#13;&#10;----------------------header--------------------------&#13;&#10;" /> <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" /> </layout> </appender> <!--HeartBeat日誌記錄--> <appender name="HeartBeatAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Logs/HeartBeat/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" /> <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="&#13;&#10;----------------------header--------------------------&#13;&#10;" /> <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" /> </layout> </appender> <!--UrlFilter日誌記錄--> <appender name="UrlFilterAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Logs/UrlFilter/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" /> <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="&#13;&#10;----------------------header--------------------------&#13;&#10;" /> <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" /> </layout> </appender> <!--NHibernate日誌記錄--> <appender name="NHibernateAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Logs/NHibernate/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value="yyyy-MM-dd&quot;.txt&quot;" /> <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="&#13;&#10;----------------------header--------------------------&#13;&#10;" /> <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" /> </layout> </appender> <!--郵箱設置--> <appender name="EmailAppender" type="log4net.Appender.SmtpAppender"> <!-- SMTP 驗證方式 --> <authentication value="Basic" /> <to value="" /> <from value="[email protected]" /> <username value="[email protected]" /> <password value="DSpuhui360.cn" /> <subject value="【本地環境】CRM後臺API錯誤異常" /> <!--郵件服務端--> <smtpHost value="smtp.exmail.qq.com"/> <bufferSize value="10240" /> <lossy value="true" /> <!--這裏我設置的是Error ,會接收(FATAL和ERROR日誌)你可以根據自己的需要設置Log4net支持多種級別的日誌。優先級從高到低依次排列如下:FATAL > ERROR > WARN > INFO > DEBUG--> <evaluator type="log4net.Core.LevelEvaluator"> <threshold value="OFF"/> </evaluator> <!--輸出格式--> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> <param name="Header" value="&#13;&#10;----------------------header--------------------------&#13;&#10;" /> <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" /> </layout> </appender> </log4net> </configuration>
View Code

Web.config

 <!--log4net日誌配置文件-->
    <add key="log4net" value="config/log4net/log.config" />

3. 註冊lognet

技術分享
  protected void Application_Start()
        {          

            //註冊lognet
            var log4NetFileName = FilePath(ConfigurationManager.AppSettings["log4net"]);
            if (File.Exists(log4NetFileName))
            {
                XmlConfigurator.ConfigureAndWatch(new FileInfo(log4NetFileName));
            }
        }
        private static string FilePath(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return string.Empty;
            }

            return string.Format("{0}/{1}", AppDomain.CurrentDomain.BaseDirectory.TrimEnd(\\, /), name.TrimStart(\\, /));
        }
View Code

4. 編寫Helper

技術分享
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Log4NetTest
{
    public static class Log4NetUtility
    {
        /// <summary>
        ///     寫日誌
        /// </summary>

        public static void WriteLog(this string message, string name, Log4NetType logType = Log4NetType.Info)
        {
            var log = GetLog(name);
            if (log == null)
            {
                return;
            }

            if (logType == Log4NetType.Debug)
            {
                log.Debug(message);
                return;
            }

            if (logType == Log4NetType.Error)
            {
                log.Error(message);
                return;
            }

            if (logType == Log4NetType.Fatal)
            {
                log.Debug(message);
                return;
            }

            if (logType == Log4NetType.Info)
            {
                log.Info(message);
                return;
            }

            log.Warn(message);
        }

        /// <summary>
        ///     寫異常日誌
        /// </summary>
        public static void WriteLog(this Exception exception, string name)
        {
            var log = GetLog(name);
            if (log == null)
            {
                return;
            }

            WriteLog(exception, log);
        }

        /// <summary>
        ///     寫異常日誌
        /// </summary>
        private static void WriteLog(Exception exception, ILog log)
        {
            if (exception == null)
            {
                return;
            }

            log.Error(exception);

            WriteLog(exception.InnerException, log);
        }

        /// <summary>
        ///     獲取日誌對象
        /// </summary>
        public static ILog GetLog(string name)
        {
            var logger = LogManager.GetLogger(name);
            return logger;
        }
    }

    /// <summary>
    ///     Log4Net類型枚舉
    /// </summary>
    public enum Log4NetType
    {
        /// <summary>
        ///     字段Debug
        /// </summary>

        Debug,

        /// <summary>
        ///     字段Error
        /// </summary>
        Error,

        /// <summary>
        ///     字段Info
        /// </summary>
        Info,

        /// <summary>
        ///     字段Fatal
        /// </summary>
        Fatal,

        /// <summary>
        ///     字段Warn
        /// </summary>
        Warn
    }

    /// <summary>
    ///     類名稱:Log4NetName
    ///     命名空間:DaiShu.Crm.Utility
    ///     類功能:日誌名稱
    /// </summary>
    public class Log4NetName
    {
        /// <summary>
        ///     字段UrlFilter
        /// </summary>     
        public const string Log4NetTest = "Log4NetTest";

        /// <summary>
        ///     字段UrlFilter
        /// </summary>       
        public const string UrlFilter = "RYJ.Crm.UrlFilter";

        /// <summary>
        ///     字段System
        /// </summary>      
        public const string System = "RYJ.Crm.System";

        /// <summary>
        ///     字段Quartz
        /// </summary>       
        public const string Quartz = "RYJ.Crm.Quartz";

        /// <summary>
        ///     字段Service
        /// </summary>       
        public const string Service = "RYJ.Crm.Services";

        /// <summary>
        ///     字段ExternalService
        /// </summary>     
        public const string ExternalService = "RYJ.Crm.ExternalService";

        /// <summary>
        ///     字段Return
        /// </summary>     
        public const string Return = "RYJ.Return";

        /// <summary>
        ///     字段HeartBeat
        /// </summary>       
        public const string HeartBeat = "RYJ.Crm.HeartBeat";

        /// <summary>
        /// 字段Event
        /// </summary>      
        public const string Event = "RYJ.Crm.Event";
    }
}
View Code

調用演示

            try
            {
                int n=int.Parse("aaa");
            }
            catch (Exception ex)
            {
                ex.WriteLog(Log4NetName.Log4NetTest);
            }

日誌記錄~log4.net