我尝试在项目中实现Topshelf.NLog
。
这是我所做的:
我已经添加到项目Topshelf.NLog
中的NuGet软件包。
之后,我编辑了App.config
并添加了:
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
在那之后,我在Program.cs
上添加了一行:
serviceConfig.UseNLog();
但是在上面这一行之后,我发现我的控制台应用程序无法正常工作。
这是我的代码:
class Program
{
static void Main(string[] args)
{
try
{
HostFactory.Run(serviceConfig =>
{
//serviceConfig.UseNLog();
serviceConfig.Service<ConverterService>(serviceInstance =>
{
serviceInstance.ConstructUsing(() => new ConverterService());
serviceInstance.WhenStarted(execute => execute.Start());
serviceInstance.WhenStopped(execute => execute.Stop());
});
serviceConfig.SetServiceName("FilesProcessor");
serviceConfig.SetDisplayName("Files Processor");
serviceConfig.SetDescription("Simple console app that works as a service.");
serviceConfig.StartAutomatically();
});
}
catch(Exception ex)
{
}
}
对//serviceConfig.UseNLog();
进行了评论,我的输出符合预期:
当我不使用serviceConfig.UseNLog();
时,控制台中没有任何内容,因此可能无法正常工作:
Here is my `App.Config`:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Products.WindowsService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="Products.BusinessLogic.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<applicationSettings>
</applicationSettings>
<nlog>
<targets>
<target name="t1" type="File" fileName="C:\WhatEverServiceLogs.txt"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="t1"/>
</rules>
</nlog>
</configuration>
答案 0 :(得分:0)
您的app.config仅包含文件目标:
<targets>
<target name="t1" type="File" fileName="C:\WhatEverServiceLogs.txt"/>
</targets>
尝试添加控制台目标:
<targets>
<target name="logfile" type="File" fileName="C:\WhatEverServiceLogs.txt" />
<target name="logconsole" type="Console" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logconsole" />
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
答案 1 :(得分:-1)
在配置结束时移动serviceConfig.UseNLog():
serviceConfig.Service<....>...
serviceConfig.UseNLog()