运行WPF应用程序的可执行文件时,log4net无法正常工作

时间:2016-07-27 14:59:44

标签: c# .net wpf log4net log4net-configuration

我在WPF应用程序中使用log4net。 log4net在从Visual Studio运行应用程序时起作用,但是,当我们通过bin的可执行文件运行应用程序时,它不起作用。

我们正在使用log4net.config文件,这是它的样子:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <log4net debug="true">
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="C:/ProgramData/CompApps/Logs/AppName/log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="25" />
      <maximumFileSize value="20MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%utcdate UTC %property{log4net:HostName} [%thread] %-5level %logger - %method() - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="Info" />
      <appender-ref ref="RollingFileAppender" />
    </root>
  </log4net>
</configuration>

log4net.config属性

Build Action: Content
Copy to Output Directory: Copy always

AssemblyInfo.cs

// log4net config
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

这在我们想要使用记录器的每个类中使用:

private static readonly ILog log = LogManager.GetLogger(typeof(App));

1 个答案:

答案 0 :(得分:1)

在代码中配置的另一种方法:

namespace Company.Product
{
    using System.Text;
    using log4net;
    using log4net.Appender;
    using log4net.Core;
    using log4net.Layout;
    using log4net.Repository.Hierarchy;

    public static class LogHelper
    {
        static LogHelper()
        {
            var hierarchy = (Hierarchy)LogManager.GetRepository();
            hierarchy.Root.Level = Level.All;
            hierarchy.Configured = true;
        }

        public static ILog GetLoggerRollingFileAppender(string logName, string fileName)
        {
            var log = LogManager.Exists(logName);

            if (log != null) return log;

            var appenderName = $"{logName}Appender";
            log = LogManager.GetLogger(logName);
            ((Logger)log.Logger).AddAppender(GetRollingFileAppender(appenderName, fileName));

            return log;
        }

        public static RollingFileAppender GetRollingFileAppender(string appenderName, string fileName)
        {
            var layout = new PatternLayout { ConversionPattern = "%date{dd.MM.yyyy HH:mm:ss.fff}  [%-5level]  %message%newline" };
            layout.ActivateOptions();

            var appender = new RollingFileAppender
            {
                Name = appenderName,
                File = fileName,
                AppendToFile = true,
                RollingStyle = RollingFileAppender.RollingMode.Size,
                MaxSizeRollBackups = 2,
                MaximumFileSize = "500KB",
                Layout = layout,
                ImmediateFlush = true,
                LockingModel = new FileAppender.MinimalLock(),
                Encoding = Encoding.UTF8,
            };

            appender.ActivateOptions();

            return appender;
        }
    }
}

您案件的用法:

private static readonly ILog log = LogHelper.GetLoggerRollingFileAppender(typeof(App), "C:/ProgramData/CompApps/Logs/AppName/log.txt");

或者从应用程序配置文件中引用日志文件的完整路径。