zoukankan      html  css  js  c++  java
  • .net core2.0 中使用log4net

    一、nuget安装log4net

    二、添加log4net.config配置文件

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <!--添加自定义节点:log4net type:解析类名,程序集名(log4net.dll)-->
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
      </configSections>
      <log4net>
        <root>
          <!--<level value="DEBUG"/>-->
          <!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF  ALL表示允许所有的日志请求。OFF是拒绝所有的请求。-->
          <!--比如定义级别为INFO,则INFO级别向下的级别,比如DEBUG日志将不会被记录-->
          <level value="ALL"/>
          <!--根据log级别记录到不同的日志文件-->
          <appender-ref ref="ErrorLog" />
        </root>
        <!--Error-->
        <appender name="ErrorLog" type="log4net.Appender.RollingFileAppender">
          <file value="Log/"/>
          <appendToFile value="true" />
          <rollingStyle value="Date" />
          <!--日期的格式,每天换一个文件记录,如不设置则永远只记录一天的日志,需设置-->
          <DatePattern value="yyyyMMdd"_Error.log"" />
          <!--日志文件名是否为静态-->
          <StaticLogFileName value="false"/>
          <!--多线程时采用最小锁定-->
          <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
          <!--布局(向用户显示最后经过格式化的输出信息)-->
          <layout type="log4net.Layout.PatternLayout">
            <ConversionPattern value="%date 线程ID:[%thread] 日志级别:%-5level 出错类:%logger property:[%property{NDC}] - 错误描述:%message %newline" />
          </layout>
          <securityContext type="log4net.Util.WindowsSecurityContext">
            <credentials value="Process" />
          </securityContext>
          <filter type="log4net.Filter.LevelRangeFilter">
            <levelMin value="DEBUG" />
            <levelMax value="ERROR" />
          </filter>
        </appender>
        <!--Error-->
      </log4net>
    </configuration>
    

    三、编写loghelper

    using GrabNovelApi;
    using log4net;
    using System;
    using System.Collections.Concurrent;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Grab.api
    {
      public static class LogHelper
        {
            private static readonly ConcurrentDictionary<Type, ILog> Loggers = new ConcurrentDictionary<Type, ILog>();
    
            /// <summary>
            /// 获取记录器
            /// </summary>
            /// <param name="source">soruce</param>
            /// <returns></returns>
            private static ILog GetLogger(Type source)
            {
                if (Loggers.ContainsKey(source))
                {
                    return Loggers[source];
                }
                else
                {
                    ILog logger = LogManager.GetLogger(Startup.repository.Name,source);
                    Loggers.TryAdd(source, logger);
                    return logger;
                }
            }
    
            /* Log a message object */
    
            /// <summary>
            /// 调试信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            public static void Debug(object source, string message)
            {
                Debug(source.GetType(), message);
            }
    
            /// <summary>
            /// 调试信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            /// <param name="ps">ps</param>
            public static void Debug(object source, string message, params object[] ps)
            {
                Debug(source.GetType(), string.Format(message, ps));
            }
    
            /// <summary>
            /// 调试信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            public static void Debug(Type source, string message)
            {
                ILog logger = GetLogger(source);
                if (logger.IsDebugEnabled)
                {
                    logger.Debug(message);
                }
            }
    
            /// <summary>
            /// 关键信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            public static void Info(object source, object message)
            {
                Info(source.GetType(), message);
            }
    
            /// <summary>
            /// 关键信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            public static void Info(Type source, object message)
            {
                ILog logger = GetLogger(source);
                if (logger.IsInfoEnabled)
                {
                    logger.Info(message);
                }
            }
    
            /// <summary>
            /// 警告信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            public static void Warn(object source, object message)
            {
                Warn(source.GetType(), message);
            }
    
            /// <summary>
            /// 警告信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            public static void Warn(Type source, object message)
            {
                ILog logger = GetLogger(source);
                if (logger.IsWarnEnabled)
                {
                    logger.Warn(message);
                }
            }
    
            /// <summary>
            /// 错误信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            public static void Error(object source, object message)
            {
                Error(source.GetType(), message);
            }
    
            /// <summary>
            /// 错误信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            public static void Error(Type source, object message)
            {
                ILog logger = GetLogger(source);
                if (logger.IsErrorEnabled)
                {
                    logger.Error(message);
                }
            }
    
            /// <summary>
            /// 失败信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            public static void Fatal(object source, object message)
            {
                Fatal(source.GetType(), message);
            }
    
            /// <summary>
            /// 失败信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            public static void Fatal(Type source, object message)
            {
                ILog logger = GetLogger(source);
                if (logger.IsFatalEnabled)
                {
                    logger.Fatal(message);
                }
            }
    
            /* Log a message object and exception */
    
            /// <summary>
            /// 调试信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            /// <param name="exception">ex</param>
            public static void Debug(object source, object message, Exception exception)
            {
                Debug(source.GetType(), message, exception);
            }
    
            /// <summary>
            /// 调试信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            /// <param name="exception">ex</param>
            public static void Debug(Type source, object message, Exception exception)
            {
                GetLogger(source).Debug(message, exception);
            }
    
            /// <summary>
            /// 关键信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            /// <param name="exception">ex</param>
            public static void Info(object source, object message, Exception exception)
            {
                Info(source.GetType(), message, exception);
            }
    
            /// <summary>
            /// 关键信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            /// <param name="exception">ex</param>
            public static void Info(Type source, object message, Exception exception)
            {
                GetLogger(source).Info(message, exception);
            }
    
            /// <summary>
            /// 警告信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            /// <param name="exception">ex</param>
            public static void Warn(object source, object message, Exception exception)
            {
                Warn(source.GetType(), message, exception);
            }
    
            /// <summary>
            /// 警告信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            /// <param name="exception">ex</param>
            public static void Warn(Type source, object message, Exception exception)
            {
                GetLogger(source).Warn(message, exception);
            }
    
            /// <summary>
            /// 错误信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            /// <param name="exception">ex</param>
            public static void Error(object source, object message, Exception exception)
            {
                Error(source.GetType(), message, exception);
            }
    
            /// <summary>
            /// 错误信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            /// <param name="exception">ex</param>
            public static void Error(Type source, object message, Exception exception)
            {
                GetLogger(source).Error(message, exception);
            }
    
            /// <summary>
            /// 失败信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            /// <param name="exception">ex</param>
            public static void Fatal(object source, object message, Exception exception)
            {
                Fatal(source.GetType(), message, exception);
            }
    
            /// <summary>
            /// 失败信息
            /// </summary>
            /// <param name="source">source</param>
            /// <param name="message">message</param>
            /// <param name="exception">ex</param>
            public static void Fatal(Type source, object message, Exception exception)
            {
                GetLogger(source).Fatal(message, exception);
            }
        }
    }
    

    四、使用

  • 相关阅读:
    minimum-path-sum
    pascals-triangle
    Java -- 二分查找
    redis缓存雪崩,击穿,穿透(copy)
    使用redis限制提交次数
    数据库的悲观锁和乐观锁
    mysql常用命令
    php压缩Zip文件和文件打包下载
    php去除数据库的数据空格
    php获取本年、本月、本周时间戳和日期格式的实例代码(分析)
  • 原文地址:https://www.cnblogs.com/WangJunZzz/p/8708423.html
Copyright © 2011-2022 走看看