• log4net用法实例 拓荒者


    1.引用log4net.dll

    2.在AssemblyInfo.cs中添加初始化:

    [assembly: log4net.Config.XmlConfigurator(ConfigFile
    = "log4net.config", Watch = true)]

    3.添加配置文件,内容如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    </configSections>
    <log4net>
    <root>
    </root>
    <logger name="OneCardRequest">
    <level value="INFO" />
    <appender-ref ref="rollingRequestFile" />
    </logger>
    <logger name="OneCardResult">
    <level value="INFO" />
    <appender-ref ref="rollingResultFile" />
    </logger>
    <logger name="OneCardException">
    <level value="ERROR" />
    <appender-ref ref="rollingExceptionFile" />
    </logger>
    <logger name="InfoLog">
    <level value="ALL" />
    <appender-ref ref="rollingInfoFile" />
    </logger>
    <appender name="rollingRequestFile" type="log4net.Appender.RollingFileAppender,log4net" >
    <param name="File" type="" value="Log/Request/" />
    <param name="AppendToFile" value="true" />
    <param name="RollingStyle" value="Date" />
    <param name="DatePattern" value="yyyyMMdd" />
    <param name="StaticLogFileName" value="false" />
    <layout type="log4net.Layout.PatternLayout,log4net">
    <param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" />
    <param name="Header" value="-----------BEGIN-----------" />
    <param name="Footer" value="------------END------------" />
    </layout>
    </appender>
    <appender name="rollingResultFile" type="log4net.Appender.RollingFileAppender,log4net" >
    <param name="File" type="" value="Log/Result/" />
    <param name="AppendToFile" value="true" />
    <param name="RollingStyle" value="Date" />
    <param name="DatePattern" value="yyyyMMdd" />
    <param name="StaticLogFileName" value="false" />
    <layout type="log4net.Layout.PatternLayout,log4net">
    <param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" />
    <param name="Header" value="-----------BEGIN-----------" />
    <param name="Footer" value="------------END------------" />
    </layout>
    </appender>
    <appender name="rollingExceptionFile" type="log4net.Appender.RollingFileAppender,log4net" >
    <param name="File" type="" value="Log/Exception/" />
    <param name="AppendToFile" value="true" />
    <param name="RollingStyle" value="Date" />
    <param name="DatePattern" value="yyyyMMdd" />
    <param name="StaticLogFileName" value="false" />
    <layout type="log4net.Layout.PatternLayout,log4net">
    <param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" />
    <param name="Header" value="-----------BEGIN-----------" />
    <param name="Footer" value="------------END------------" />
    </layout>
    </appender>
    <appender name="rollingInfoFile" type="log4net.Appender.RollingFileAppender,log4net" >
    <param name="File" type="" value="Log/Info/" />
    <param name="AppendToFile" value="true" />
    <param name="RollingStyle" value="Date" />
    <param name="DatePattern" value="yyyyMMdd" />
    <param name="StaticLogFileName" value="false" />
    <layout type="log4net.Layout.PatternLayout,log4net">
    <param name="ConversionPattern" value="%n[%d] %c %n%m%n%n" />
    <param name="Header" value="-----------BEGIN-----------" />
    <param name="Footer" value="------------END------------" />
    </layout>
    </appender>
    </log4net>
    </configuration>


    4.添加Log4netHelper类:

    public class log4netHelper
    {
    /// <summary>
    /// 追加一条一卡通处理异常日志信息
    /// </summary>
    /// <param name="id">请求ID</param>
    /// <param name="ex">异常</param>
    /// <param name="attachMessage">附加信息</param>
    public static void Exception(string id, Exception ex, string attachMessage)
    {
    log4net.ILog log
    = log4net.LogManager.GetLogger("OneCardException");
    if (log.IsErrorEnabled)
    {
    string strMessage = string.Format("RequestID:{0}\r\nMesssage:{1}\r\nStackTrace:{2}\r\nAttachMessage:{3}", id, ex.Message, ex.StackTrace, attachMessage);
    log.Error(strMessage);
    }
    log
    = null;
    }
    /// <summary>
    /// 追加一条一卡通请求日志信息
    /// </summary>
    /// <param name="id">请求ID</param>
    /// <param name="ip">请求的IP地址</param>
    /// <param name="datetime">时间</param>
    /// <param name="strParam">请求时传入的参数</param>
    public static void RequestLog(string id, string ip, string datetime, string strParam)
    {
    log4net.ILog log
    = log4net.LogManager.GetLogger("OneCardRequest");
    if (log.IsInfoEnabled)
    {
    string strMessage = string.Format("RequestID:{0}\r\nIP:{1}\r\nDateTime:{2}\r\nParam:{3}", id, ip, datetime, strParam);
    log.Info(strMessage);
    }
    log
    = null;
    }
    /// <summary>
    /// 追加一条一卡通处理结果日志
    /// </summary>
    /// <param name="id">请求ID</param>
    /// <param name="datetime">时间</param>
    /// <param name="strResult">处理结果</param>
    public static void ResultLog(string id, string datetime, string strResult)
    {
    log4net.ILog log
    = log4net.LogManager.GetLogger("OneCardResult");
    if (log.IsInfoEnabled)
    {
    string strMessage = string.Format("RequestID:{0}\r\nDateTime:{1}\r\nParam:{2}", id, datetime, strResult);
    log.Info(strMessage);
    }
    log
    = null;
    }
    /// <summary>
    /// 追加一条普通的日志信息
    /// </summary>
    /// <param name="message">日志内容</param>
    public static void Info(string message)
    {
    log4net.ILog log
    = log4net.LogManager.GetLogger("InfoLog");
    if (log.IsInfoEnabled)
    {
    log.Info(message);
    }
    log
    = null;
    }

    }
    2021年9月 北京、西安两地,高薪诚聘 .NET工程师,请私信联系!
    如果认为此文对您有帮助,别忘了支持一下哦!
    声明:本博客原创文字只代表本人工作中在某一时间内总结的观点或结论,与本人所在单位没有直接利益关系。转载时请在文章页面明显位置给出原文链接。
  • 相关阅读:
    Eclipse汉化教程
    php课程---文件操作及文件上传的代码总结
    php课程---Json格式规范需要注意的小细节
    php课程---php使用PDO方法详解(转)
    php课程---随机数
    php课程---Ajax(老师详解)
    php课程---JavaScript与Jquery的区别
    php课程---JavaScript与Jquery的区别(转)
    php课程---初学PDO
    php课程---练习(联系人信息表)
  • 原文地址:https://www.cnblogs.com/youring2/p/2030424.html
Copyright © 2020-2023  润新知