2

我试图Topshelf.NLog在我的项目中实施。

这是我所做的:

通过我添加到项目中的 NuGet 包Topshelf.NLog

之后,我编辑了我的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>
4

1 回答 1

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>

另见:https ://github.com/NLog/NLog/wiki/Tutorial

于 2020-01-05T18:48:58.563 回答