• C# log4net


    1.install-package log4net

    2.add new config file, its name is log4net.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <log4net>
    <root>
    <level value="ALL" />
    <appender-ref ref="console" />
    <appender-ref ref="file" />
    </root>
    <appender name="console" type="log4net.Appender.ColoredConsoleAppender">
    <mapping>
    <level value="ERROR" />
    <foreColor value="Red, HighIntensity" />
    </mapping>
    <mapping>
    <level value="INFO"/>
    <foreColor value="Green,HighIntensity"/>
    </mapping>
    <mapping>
    <level value="Debug"/>
    <foreColor value="Yellow,HighIntensity"/>
    </mapping>
    <mapping>
    <level value="Warn"/>
    <foreColor value="Purple,HighIntensity"/>
    </mapping>
    <mapping>
    <level value="Fatal"/>
    <foreColor value="Cyan,HighIntensity"/>
    </mapping>
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %level %logger - %message%newline" />
    </layout>
    </appender>
    <appender name="file" type="log4net.Appender.RollingFileAppender">
    <file value="myapp.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="5" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
    </layout>
    </appender>
    </log4net>
    </configuration>

    3.Right click the log4net.config and set Copy to Output Directory as 'Copy Always'

    4.Open AssemblyInfo.cs and append to the end

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

    5. 

     private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            static void Main(string[] args)
            {
                Log4netDemo();
                Console.ReadLine();
            }
    
            static void Log4netDemo()
            {           
                logger.Info($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}");            
                logger.Error($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
                logger.Debug($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
                logger.Fatal($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
                logger.Warn($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}");             
            }

    Of course,you can set the file location manually as below

    <file type="log4net.Util.PatternString" value="Logs\Common\%date{yyyy-MM-dd-HH-mm-ss-ffff}.log" />

    or

    <file type="log4net.Util.PatternString" value="./Logs//Common//%date{yyyy-MM-dd-HH-mm-ss-ffff}.log" />

    <appender name="file" type="log4net.Appender.RollingFileAppender">     
          <file type="log4net.Util.PatternString" value="Logs\Common\%date{yyyy-MM-dd-HH-mm-ss-ffff}.log" />
          <appendToFile value="true" />
          <rollingStyle value="Size" />
          <maxSizeRollBackups value="5" />
          <maximumFileSize value="10MB" />
          <staticLogFileName value="true" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
          </layout>
        </appender>
  • 相关阅读:
    SharePoint on Windows Server 2008 R2 "客户端不支持使用windows资源管理器打开此列表”
    Exception:Collection was modified; enumeration operation may not execute.
    windows 2008 iisapp
    VMware Workstation OEM (MultiBrand) Bios Installer
    Cajviewer 7.0.2 Windows 7下使用方法:
    Delphi与Windows 7下的用户账户控制(UAC)机制
    VS2008 在WINDOWS 7的UAC开启的情况下如何写注册表HKLM(HKEY_LOCAL_MACHIN)下的值
    windows SDK安装出现没有权限的一个解决办法
    Thinkpad Marker SLIC 2.1 激活Windows 7的详细教程
    点击网页中PDF链接时,打开PDF文档不再是直接IE查看,而是提示下载保存
  • 原文地址:https://www.cnblogs.com/augustuss/p/12769852.html
Copyright © 2020-2023  润新知