C#中使用log4net框架做日志输出

C#中使用log4net框架做日志输出

一、用法

1、引入包:https://www.nuget.org/packages/log4net/

2、Main函数

using System;
using System.IO;
using log4net;
using log4net.Config;

namespace Log4NetDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var repository = LogManager.CreateRepository("NETCoreRepository");
            XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
            var log = LogManager.GetLogger("NETCoreRepository", typeof(Program));

            log.Info("this is a info.");
            log.Error("this is an error:", new Exception("this is error."));
            log.Fatal("this is a fatal.");
            log.Warn("this is a warn");
            Console.WriteLine("finish.");
        }
    }
}

如果不想创建Repository的话,也可以写个工具类,统一在名称空间上备注完成。

using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = false)]
namespace MyProj.Utils.Log
{
    public class LogUtils
    {
        private ILog log = LogManager.GetLogger(typeof(LogUtils));

        public void Debug(object message) => log.Debug(message);
        public void Debug(object message, Exception exception) => log.Debug(message, exception);
        public void DebugFormat(string format, params object[] args) => log.DebugFormat(format, args);
        public void DebugFormat(string format, object arg0) => log.DebugFormat(format, arg0);
        public void DebugFormat(string format, object arg0, object arg1, object arg2) => log.DebugFormat(format, arg0, arg1, arg2);
        public void DebugFormat(string format, object arg0, object arg1) => log.DebugFormat(format, arg0, arg1);
        public void Error(object message) => log.Error(message);
        public void Error(object message, Exception exception) => log.Error(message, exception);
        public void ErrorFormat(string format, object arg0, object arg1, object arg2) => log.ErrorFormat(format, arg0, arg1, arg2);
        public void ErrorFormat(string format, object arg0, object arg1) => log.ErrorFormat(format, arg0, arg1);
        public void ErrorFormat(string format, object arg0) => log.ErrorFormat(format, arg0);
        public void ErrorFormat(string format, params object[] args) => log.ErrorFormat(format, args);
        public void Fatal(object message) => log.Fatal(message);
        public void Fatal(object message, Exception exception) => log.Fatal(message, exception);
        public void FatalFormat(string format, object arg0, object arg1, object arg2) => log.FatalFormat(format, arg0, arg1, arg2);
        public void FatalFormat(string format, object arg0) => log.FatalFormat(format, arg0);
        public void FatalFormat(string format, params object[] args) => log.FatalFormat(format, args);
        public void FatalFormat(string format, object arg0, object arg1) => log.FatalFormat(format, arg0, arg1);
        public void Info(object message, Exception exception) => log.Info(message, exception);
        public void Info(object message) => log.Info(message);
        public void InfoFormat(string format, object arg0, object arg1, object arg2) => log.InfoFormat(format, arg0, arg1, arg2);
        public void InfoFormat(string format, object arg0, object arg1) => log.InfoFormat(format, arg0, arg1);
        public void InfoFormat(string format, object arg0) => log.InfoFormat(format, arg0);
        public void InfoFormat(string format, params object[] args) => log.InfoFormat(format, args);
        public void Warn(object message) => log.Warn(message);
        public void Warn(object message, Exception exception) => log.Warn(message, exception);
        public void WarnFormat(string format, object arg0, object arg1) => log.WarnFormat(format, arg0, arg1);
        public void WarnFormat(string format, object arg0) => log.WarnFormat(format, arg0);
        public void WarnFormat(string format, params object[] args) => log.WarnFormat(format, args);
        public void WarnFormat(string format, object arg0, object arg1, object arg2) => log.WarnFormat(format, arg0, arg1, arg2);
    }
}

3、新建一个log4net.config配置文件,并设置输出到程序目录下。

<?xml version="1.0" encoding="UTF-8"?>

<configuration status="ON">
    <log4net>
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
            <layout type="log4net.Layout.PatternLayout" value="%date [%thread] %-5level %logger - %message%newline" />
        </appender>
        <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
            <file value="logs/" />
            <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>
        <root level="ALL">
            <appender-ref ref="ConsoleAppender" />
            <appender-ref ref="RollingLogFileAppender" />
        </root>
    </log4net>
</configuration>

4、效果:


C#中使用log4net框架做日志输出
https://www.dearcloud.cn/2018/05/10/20200310-cnblogs-old-posts/20180510-CSharp中使用log4net框架做日志输出/
作者
宋兴柱
发布于
2018年5月10日
许可协议