Topshelf log4net无法正常工作

时间:2016-02-17 14:31:16

标签: c# log4net topshelf

我有一个使用Topshelf和Log4net的项目,当我运行项目时,我看到一个新的日志文件,其中只包含有关服务创建,启动等的信息,我手动尝试的日志条目都没有添加到文件中。

的App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" requirePermission="false"/>
</configSections>
<startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Common.Logging.Core" publicKeyToken="af08829b84f0328e" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Topshelf" publicKeyToken="b800c4cfcdeea87b" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.3.154.0" newVersion="3.3.154.0" />
  </dependentAssembly>
</assemblyBinding>
</runtime>

<log4net>

<appender name="LogFileAppender"
      type="log4net.Appender.RollingFileAppender" >
  <param name="AppendToFile"
       value="true" />
  <file type="log4net.Util.PatternString" value="..\..\log\Topshelf.Host.%appdomain.log" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="4" />
  <maximumFileSize value="10MB" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern"
         value="%-5p %d{yyyy-MM-dd hh:mm:ss} - %m%n" />
  </layout>
</appender>
<root>
  <level value="ALL" />
  <appender-ref ref="LogFileAppender" />
</root>
</log4net>
</configuration>

Program.cs的

public class Program
{
    private static readonly ILog log = LogManager.GetLogger(typeof(Job));

    static void Main(string[] args)
    {
        HostFactory.Run(c =>                                 
        {
            log4net.Config.XmlConfigurator.Configure();

            c.UseLog4Net();

            c.ScheduleQuartzJobAsService(q =>
                    q.WithJob(() =>
                        JobBuilder.Create<Job>().Build())
                    .AddTrigger(() =>
                        TriggerBuilder.Create()
                            .WithSimpleSchedule(builder => builder
                                .WithIntervalInSeconds(5)
                                .RepeatForever())
                            .Build())
                                );       

            log.Info("Setting up the service");
        });           
    }
}

Job.cs

public class Job : IJob
{
    private readonly ILog log = LogManager.GetLogger(typeof(Job));

    public void Execute(IJobExecutionContext context)
    {
        log.ErrorFormat("The current time is: {0}", DateTime.Now);            
    }
}

1 个答案:

答案 0 :(得分:4)

您的配置似乎没问题,因此最常见的错误是您无权在日志记录目录中写入:..\..\log\Topshelf.Host.%appdomain.log