• Log4Net


    引入log4net.dll文件

    1,Log4NetHelper

      1 using log4net;
      2 using System;
      3 using System.Collections.Concurrent;
      4 //Specifies the config file used by log4net to read the configuration information
      5 [assembly: log4net.Config.XmlConfigurator(ConfigFile = @"Log4Net.config", Watch = true)]
      6 namespace Quality.Tools.Common.Log
      7 {
      8     /// <summary>
      9     /// Log4Net Helper
     10     /// </summary>
     11     public class Log4NetHelper
     12     {
     13         private static readonly ConcurrentDictionary<Type, ILog> _loggers = new ConcurrentDictionary<Type, ILog>();
     14         private readonly Type _type = null;
     15         public Log4NetHelper(Type type)
     16         {
     17             _type = type;
     18         }
     19 
     20         #region Get Logger
     21         /// <summary>
     22         /// Get Logger
     23         /// </summary>
     24         /// <param name="source"></param>
     25         /// <returns></returns>
     26         private static ILog GetLogger(Type source)
     27         {
     28             if (_loggers.ContainsKey(source))
     29             {
     30                 return _loggers[source];
     31             }
     32             else
     33             {
     34                 ILog logger = LogManager.GetLogger(source);
     35                 _loggers.TryAdd(source, logger);
     36                 return logger;
     37             }
     38         } 
     39         #endregion
     40 
     41         #region Debug
     42 
     43         /// <summary>
     44         /// Debug
     45         /// </summary>
     46         /// <param name="source">source</param>
     47         /// <param name="message">message</param>
     48         public static void Debug(Type source, object message)
     49         {
     50             ILog logger = GetLogger(source);
     51             if (logger.IsDebugEnabled)
     52                 logger.Debug(message);
     53         }
     54 
     55         /// <summary>
     56         /// Debug
     57         /// </summary>
     58         /// <param name="source">source</param>
     59         /// <param name="message">message</param>
     60         /// <param name="exception">exception</param>
     61         public static void Debug(Type source, object message, Exception exception)
     62         {
     63             GetLogger(source).Debug(message, exception);
     64         }
     65 
     66         /// <summary>
     67         /// Debug
     68         /// </summary>
     69         /// <param name="message">message</param>
     70         public void Debug(object message)
     71         {
     72             Debug(_type, message);
     73         }
     74 
     75         /// <summary>
     76         /// Debug
     77         /// </summary>
     78         /// <param name="message">message</param>
     79         /// <param name="exception">exception</param>
     80         public void Debug(object message, Exception exception)
     81         {
     82             Debug(_type, message, exception);
     83         }
     84         #endregion
     85 
     86         #region Warn
     87 
     88         /// <summary>
     89         /// Warn
     90         /// </summary>
     91         /// <param name="source">source</param>
     92         /// <param name="message">message</param>
     93         public static void Warn(Type source, object message)
     94         {
     95             ILog logger = GetLogger(source);
     96             if (logger.IsWarnEnabled)
     97                 logger.Warn(message);
     98         }
     99 
    100         /// <summary>
    101         /// Warn
    102         /// </summary>
    103         /// <param name="source">source</param>
    104         /// <param name="message">message</param>
    105         /// <param name="exception">exception</param>
    106         public static void Warn(Type source, object message, Exception exception)
    107         {
    108             GetLogger(source).Warn(message, exception);
    109         }
    110 
    111         /// <summary>
    112         /// Warn
    113         /// </summary>
    114         /// <param name="message">message</param>
    115         public void Warn(object message)
    116         {
    117             Warn(this._type, message);
    118         }
    119 
    120         /// <summary>
    121         /// Warn
    122         /// </summary>
    123         /// <param name="message">message</param>
    124         /// <param name="exception">exception</param>
    125         public void Warn(object message, Exception exception)
    126         {
    127             Warn(this._type, message, exception);
    128         }
    129         #endregion
    130 
    131         #region Info
    132         /// <summary>
    133         /// Info
    134         /// </summary>
    135         /// <param name="source">source</param>
    136         /// <param name="message">message</param>
    137         public static void Info(Type source, object message)
    138         {
    139             ILog logger = GetLogger(source);
    140             if (logger.IsInfoEnabled)
    141                 logger.Info(message);
    142         }
    143 
    144         /// <summary>
    145         /// Info
    146         /// </summary>
    147         /// <param name="source">source</param>
    148         /// <param name="message">message</param>
    149         /// <param name="exception">exception</param>
    150         public static void Info(Type source, object message, Exception exception)
    151         {
    152             GetLogger(source).Info(message, exception);
    153         }
    154 
    155         /// <summary>
    156         /// Info
    157         /// </summary>
    158         /// <param name="message">message</param>
    159         public void Info(object message)
    160         {
    161             Info(this._type, message);
    162         }
    163 
    164         /// <summary>
    165         /// Info
    166         /// </summary>
    167         /// <param name="message">message</param>
    168         /// <param name="exception">exception</param>
    169         public void Info(object message, Exception exception)
    170         {
    171             Info(this._type, message, exception);
    172         }
    173         #endregion
    174 
    175         #region Error
    176         /// <summary>
    177         /// Error
    178         /// </summary>
    179         /// <param name="source">source</param>
    180         /// <param name="message">message</param>
    181         public static void Error(Type source, object message)
    182         {
    183             ILog logger = GetLogger(source);
    184             if (logger.IsErrorEnabled)
    185                 logger.Error(message);
    186         }
    187 
    188         /// <summary>
    189         /// Error
    190         /// </summary>
    191         /// <param name="source">source</param>
    192         /// <param name="message">message</param>
    193         /// <param name="exception">exception</param>
    194         public static void Error(Type source, object message, Exception exception)
    195         {
    196             GetLogger(source).Error(message, exception);
    197         }
    198 
    199         /// <summary>
    200         /// Error
    201         /// </summary>
    202         /// <param name="message">message</param>
    203         public void Error(object message)
    204         {
    205             Error(this._type, message);
    206         }
    207 
    208         /// <summary>
    209         /// Error
    210         /// </summary>
    211         /// <param name="message">message</param>
    212         /// <param name="exception">exception</param>
    213         public void Error(object message, Exception exception)
    214         {
    215             Error(this._type, message, exception);
    216         }
    217         #endregion
    218 
    219         #region Fatal
    220         /// <summary>
    221         /// Fatal
    222         /// </summary>
    223         /// <param name="source">source</param>
    224         /// <param name="message">message</param>
    225         public static void Fatal(Type source, object message)
    226         {
    227             ILog logger = GetLogger(source);
    228             if (logger.IsFatalEnabled)
    229                 logger.Fatal(message);
    230         }
    231 
    232         /// <summary>
    233         /// Fatal
    234         /// </summary>
    235         /// <param name="source">source</param>
    236         /// <param name="message">message</param>
    237         /// <param name="exception">exception</param>
    238         public static void Fatal(Type source, object message, Exception exception)
    239         {
    240             GetLogger(source).Fatal(message, exception);
    241         }
    242 
    243         /// <summary>
    244         /// Fatal
    245         /// </summary>
    246         /// <param name="message">message</param>
    247         public void Fatal(object message)
    248         {
    249             Fatal(this._type, message);
    250         }
    251 
    252         /// <summary>
    253         /// Fatal
    254         /// </summary>
    255         /// <param name="message">message</param>
    256         /// <param name="exception">exception</param>
    257         public void Fatal(object message, Exception exception)
    258         {
    259             Fatal(this._type, message, exception);
    260         }
    261         #endregion
    262     }
    263 }
    Log4NetHelper

    2,Log4Net.config

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <configuration>
     3   <configSections>
     4     <!--添加自定义节点:log4net  type:解析类名,程序集名(log4net.dll)-->
     5     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
     6   </configSections>
     7 
     8   <log4net>
     9     <!--定义输出到文件中-->
    10     <appender name="Log4Net_INFO" type="log4net.Appender.RollingFileAppender">
    11       <!--定义文件存放位置-->
    12       <file value="Log/Info/"/>
    13       <!--是否追加到文件,默认为true,通常无需设置-->
    14       <appendToFile value="true"/>
    15       <RollingStyle value="Date"/>
    16       <!--日期的格式,每月一个文件夹,每天换一个文件记录,如不设置则永远只记录一天的日志,需设置-->
    17       <DatePattern value="yyyy-MM/yyyy-MM-dd&quot;.log&quot;" />
    18       <!--日志文件名是否为静态-->
    19       <StaticLogFileName value="false"/>
    20       <!--多线程时采用最小锁定-->
    21       <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    22       <!--布局(向用户显示最后经过格式化的输出信息)-->
    23       <layout type="log4net.Layout.PatternLayout">
    24         <!--
    25            %m(message):输出的日志消息,如ILog.Debug(…)输出的一条消息 
    26            %n(new line):换行 
    27            %d(datetime):输出当前语句运行的时刻 
    28            %r(run time):输出程序从运行到执行到当前语句时消耗的毫秒数 
    29            %t(thread id):当前语句所在的线程ID 
    30            %p(priority): 日志的当前优先级别,即DEBUG、INFO、WARN…等 
    31            %c(class):当前日志对象的名称,例如:
    32            %L:输出语句所在的行号 
    33            %F:输出语句所在的文件名 
    34            %-数字:表示该项的最小长度,如果不够,则用空格填充
    35           -->
    36         <Header value="[Header]&#13;&#10;"/>
    37         <Footer value="[Footer]&#13;&#10;"/>
    38         <!--正文-->
    39         <ConversionPattern value="%date ThreadID:[%thread] Level:%-5level Logger:%logger property:[%property{NDC}] %nMessage:%message%newline"  />
    40       </layout>
    41       <filter type="log4net.Filter.LevelRangeFilter">
    42         <levelMin value="Debug" />
    43         <levelMax value="Warn" />
    44       </filter>
    45     </appender>
    46 
    47     <appender name="Log4Net_ERROR" type="log4net.Appender.RollingFileAppender">
    48       <file value="Log/Error/"/>
    49       <appendToFile value="true"/>
    50       <RollingStyle value="Date"/>
    51       <DatePattern value="yyyy-MM/yyyy-MM-dd&quot;.log&quot;" />
    52       <StaticLogFileName value="false"/>
    53       <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    54       <layout type="log4net.Layout.PatternLayout">
    55         <Header value="[Header]&#13;&#10;"/>
    56         <Footer value="[Footer]&#13;&#10;"/>
    57         <!--正文-->
    58         <ConversionPattern value="%date ThreadID:[%thread] Level:%-5level Logger:%logger property:[%property{NDC}] %nMessage:%message%newline"  />
    59       </layout>
    60       <filter type="log4net.Filter.LevelRangeFilter">
    61         <levelMin value="ERROR" />
    62         <levelMax value="FATAL" />
    63       </filter>
    64     </appender>
    65 
    66     <root>
    67       <level value="ALL"/>
    68       <appender-ref ref="Log4Net_INFO" />
    69       <appender-ref ref="Log4Net_ERROR" />
    70     </root>
    71 
    72   </log4net>
    73 
    74 </configuration>
    View Code

    3,使用方式,项目添加log4net.dll引用,添加Log4Net.config配置,属性设为“Copy always”,文件结构如下:

     

     

  • 相关阅读:
    flume和kafka整合(转)
    Flume目录
    Flume的安装配置
    HBase系统架构及数据结构(转)
    toolbar ,textfield,图片拉伸,Bundle
    poj 1017 Packets
    jQuery使用serialize(),serializeArray()方法取得表单数据+字符串和对象类型两种表单提交的方法
    Android数据加密概述及多种加密方式 聊天记录及账户加密 提供高质量的数据保护
    LintCode-落单的数 III
    LeetCode90:Subsets II
  • 原文地址:https://www.cnblogs.com/shangec/p/12792602.html
Copyright © 2020-2023  润新知