未创建 log4Net 文件

log4Net File not created

App.Config 文件

    <log4net>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
        <file type="log4net.Util.PatternString" value="%property[LogFileName]"/>
        <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
        <appendToFile value="true"/>
        <rollingStyle value="Size"/>
        <maxSizeRollBackups value="10000"/>
        <maximumFileSize value="10MB"/>
        <staticLogFileName value="true"/>
        <layout type="log4net.Layout.PatternLayout">
            <param name="CoversionPattern" value="%date [%thread] : %message%newline%exception"/>
        </layout>
    </appender>
    <root>
        <level value="ALL"></level>
        <appender-ref ref="LogFileAppender"/>
    </root>
</log4net>

我在 assemblyinfo.cs 中添加了

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

app.config 中的错误是什么我没有得到日志文件,而且我在写入日志文件时也没有收到任何错误。那是 windows 应用程序。

我在Bin文件夹中搜索文件是否正确?

更改 app.config 行:

<file type="log4net.Util.PatternString" value="%property[LogFileName]"/>

到这个

<file type="log4net.Util.PatternString" value=".\%property{LogFileName}" />

然后确保在 globalcontext 中设置 logFilename,示例代码如下所示:

static void Main(string[] args)
    {
        log4net.GlobalContext.Properties["LogFileName"] = "log.txt";

        log4net.ILog logger = log4net.LogManager.GetLogger("Tests");

        logger.Debug("Test message");
    }

更多信息请参考:https://www.codeproject.com/Tips/400224/Multiple-log-files-from-one-application-using-log

根据构建配置 - debug/release,您应该会在 BIN/debug 或 BIN/release 文件夹中看到 log.txt。

您在配置中有 2 个错误:

变化:

<param name="CoversionPattern" value="%date [%thread] : %message%newline%exception"/>

收件人:

<conversionPattern value="%date{ABSOLUTE} [%logger] %level - %message%newline%exception"/>

变化:

 <file type="log4net.Util.PatternString" value="%property[LogFileName]"/>

收件人:

<file type="log4net.Util.PatternString" value="C:\MyLogs\%property{LogFileName}.txt"/>

来自Log4net sdk refference

The key to lookup must be specified within braces and directly following the pattern specifier, e.g. %property{user}

总工作 appender 配置:

<appender name="RollingFileAppender" type="log4net.appender.RollingFileAppender">
            <file type="log4net.Util.PatternString" value="C:\MyLogs\%property{LogFileName}.txt"/>
            <appendToFile value="true"/>
            <rollingStyle value="Size"/>
            <maximumFileSize value="10MB"/>
            <maxSizeRollBackups value="5"/>
            <staticLogFileName value="true"/>
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date{ABSOLUTE} [%logger] %level - %message%newline%exception"/>
            </layout>
        </appender>

C# 代码:

using System;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace Log4NetConsoleApplication
{
    class Program
    {

        static void Main(string[] args)
        {
            log4net.GlobalContext.Properties["LogFileName"] = "log";    

            log4net.ILog log = LogHelper.GetLogger();

            Console.WriteLine("hello world");

            log.Error("This is my error message");

            Console.ReadLine();
        }
    }
}