美文网首页
在VSCode写.netCore 踩坑记三(log4net日志配

在VSCode写.netCore 踩坑记三(log4net日志配

作者: 有头发的搬砖员 | 来源:发表于2021-03-23 15:19 被阅读0次

    1、在vscode内输入快捷键 ctrl + shift + P
    选择 “NuGet Packge Manager:add package”
    安装包:

    log4net
    

    2、创建一个helpers文件夹用于存放log类


    根目录创建 helpers 文件夹

    复制以下两个文件到项目上
    1、log4net.config 文件存放在根目录

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <!-- This section contains the log4net configuration settings -->
      <log4net>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
          <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
        </appender>
        
        <appender name="FileAppender" type="log4net.Appender.FileAppender">
          <file value="log-file.log" />
          <appendToFile value="true" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
          </layout>
        </appender>
     
        <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
          <file value="logfile/" />
          <appendToFile value="true" />
          <rollingStyle value="Composite" />
          <staticLogFileName value="false" />
          <datePattern value="yyyyMMdd'.log'" />
          <maxSizeRollBackups value="10" />
          <maximumFileSize value="1MB" />
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
          </layout>
        </appender>
     
        <!-- Setup the root category, add the appenders and set the default level -->
        <root>
          <level value="ALL" />
          <appender-ref ref="ConsoleAppender" />
          <appender-ref ref="FileAppender" />
          <appender-ref ref="RollingLogFileAppender" />
        </root>
     
      </log4net>
    </configuration>
    

    2、LogHelper.cs 文件存放在 helpers 文件夹内

    using log4net;
    using log4net.Config;
    using log4net.Repository;
    using System.IO;
     
    //按你实际情况修改命名空间
    namespace project.helpers
    {
        public class LogHelper
        {
            private static ILoggerRepository repository { get; set; }
            private static ILog _log;
            private static ILog log {
                get
                {
                    if (_log == null)
                    {
                        Configure();
                    }
                    return _log;
                }
            }
            
            public static void Configure(string repositoryName = "NETCoreRepository", string configFile = "log4net.config")
            {
                repository = LogManager.CreateRepository(repositoryName);
                XmlConfigurator.Configure(repository, new FileInfo(configFile));
                _log = LogManager.GetLogger(repositoryName, "");
            }
     
            public static void Info(string msg)
            {
                log.Info(msg);
            }
     
            public static void Warn(string msg)
            {
                log.Warn(msg);
            }
     
            public static void Error(string msg)
            {
                log.Error(msg);
            }
        }
    }
    

    配置后当前目录结构


    当前目录结构

    修改 Program.cs 文件

    using System;
    using System.IO;
    using Microsoft.Extensions.Configuration;
    using project.helpers;
    namespace project
    {
        class Program
        {
            static void Main(string[] args)
            {
                IConfigurationBuilder builder  = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
                IConfigurationRoot configurationRoot = builder.Build();
                String setting = configurationRoot.GetSection("setting").Value;
                Console.WriteLine(setting);
    
                //LogHelper 用的是单例模式,不需要new对象,直接调用即可
                LogHelper.Info("this is Info");
                LogHelper.Warn("this is Warn");
                LogHelper.Error("this is Error");
            }
        }
    }
    
    
    dotnet run 后的效果
    自动在生成目录创建 logfile 文件记录日志

    ---------------------------------------重点,重点来了-------------------------------------

    build时.netcore不会帮你生成config配置文件(如果有办法请指正),必须自行把log4net.config 复制到生成项目的根目录上


    发布时,不要忘记把配置文件复制过来

    相关文章

      网友评论

          本文标题:在VSCode写.netCore 踩坑记三(log4net日志配

          本文链接:https://www.haomeiwen.com/subject/tssfhltx.html