c# – 如何正确使用Topshelf.Logging

任何线索如何正确使用Topshelf.Logging?

我是否必须将N​​LogLogWriter传递给服务类的构造函数?

以及如何启用输出到控制台?

class Program
    {
        #region Properties
        Topshelf.Logging.NLogLogWriter logger;
        static string mainLoggerName = "MainLogger";
        #endregion

        static void Main(string[] args)
        {
            var  nlogLogger = LogManager.GetCurrentClassLogger();
            Topshelf.Logging.NLogLogWriter logger = new Topshelf.Logging.NLogLogWriter(nlogLogger, mainLoggerName);


            HostFactory.Run(x =>                                 
            {
                x.Service<ExSPCAgentService>(s =>                         
                {
                    s.ConstructUsing(name => new MyAgentService());      

                    // s.WhenStarted(tc => tc.Start());               
                    s.WhenStarted(tc =>
                    {
                        // Add more config options if you need
                        tc.Start();
                    });
                    s.WhenStopped(tc => tc.Stop());                
                });
                x.RunAsLocalSystem();                             
                x.UseNLog();
                x.SetDescription("MyAgentService");         
                x.SetDisplayName("MyAgentService");                        
                x.SetServiceName("MyAgentService");                        

            });
        }
    }
要指定记录器,请使用允许您指定 LogFactoryoverload of UseNLog.

要登录到控制台,您将启用console target.

编辑:docs

NLog Integration

To enable logging via NLog, the Topshelf.NLog NuGet package is available. Once added to your project, configure Topshelf to use NLog via the configuration:

HostFactory.New(x =>
{
    x.UseNLog();
});

This will change the HostLogger to use NLog. An existing LogFactory can be passed as well, using an overload of the same method.

相关文章
相关标签/搜索