How to use Topshelf.Logging correctly

Any hint on how to use Topshelf.Logging correctly?

Should I pass NLogLogWriter the class of service constructor?

And how to enable console output?

 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"); }); } } 
+5
source share
1 answer

To specify your registrar, use the UseNLog overload, which allows you to specify LogFactory .

To enter the console, you must enable the target console .

Edit: Documents

Integration with NLog

To enable logging through NLog, the NuGet Topshelf.NLog package is available. After adding to your project, configure Topshelf to use NLog through the configuration:

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

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

+4
source

Source: https://habr.com/ru/post/1244358/


All Articles