如何正确使用Topshelf.Logging
?
我是否必须将NLogLogWriter
传递给服务类的构造函数?
如何启用输出到控制台?
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");
});
}
}
答案 0 :(得分:4)
要指定记录器,请使用允许您指定overload of UseNLog的LogFactory。
要登录到控制台,您可以启用console target。
修改:docs
NLog集成
要通过NLog启用日志记录,可以使用Topshelf.NLog NuGet包。添加到项目后,配置Topshelf以通过配置使用NLog:
HostFactory.New(x =>
{
x.UseNLog();
});
这将更改HostLogger以使用NLog。现有的LogFactory也可以使用相同方法的重载传递。