问题描述
我正在使用 TopShelf 来促进构建 Windows 服务,同时还尝试正确配置 Nlog。
这是我的 Program 类,它用作我的应用程序的入口点:
class Program
{
static void Main(string[] args)
{
var services = ConfigureServices();
var serviceProvider = services.BuildServiceProvider();
var rc = HostFactory.Run(x =>
{
x.UseNLog();
x.Service<Service>(s =>
{
s.ConstructUsing(() => serviceProvider.GetService<Service>());
s.WhenStarted(cnc => cnc.Start());
s.WhenStopped(cnc => cnc.Stop());
});
x.RunAsLocalSystem();
x.SetDescription("Sample Topshelf Host");
x.SetdisplayName("Stuff");
x.SetServiceName("Stuff");
});
var exitCode = (int)Convert.ChangeType(rc,rc.GetTypeCode());
Environment.ExitCode = exitCode;
}
private static IServiceCollection ConfigureServices()
{
IServiceCollection services = new ServiceCollection();
var config = LoadConfiguration();
//Get the NLog configuration from AppSettings
LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog"));
Logger nloglogger = LogManager.GetCurrentClassLogger();
//registered services here
services.AddTransient<IInjected,Injected>();
return services;
}
public static IConfiguration LoadConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json",optional: true,reloadOnChange: true);
return builder.Build();
}
}
这是 TopShelf 正在启动的类
public class Service
{
private readonly IInjected _injected;
private Timer _timer;
public Service(IInjected injected)
{
_injected = injected
}
public void Start()
{
_timer = new Timer(1200);
_timer.Elapsed += new ElapsedEventHandler(Onelapsedtime);
_timer.AutoReset = false;
_timer.Enabled = true;
_timer.Start();
}
public void Stop()
{
Console.WriteLine("Service stopped");
_timer.AutoReset = false;
_timer.Enabled = false;
}
private void Onelapsedtime(object sender,ElapsedEventArgs e)
{
try
{
_injected.Process();
}
catch (Exception ex)
{
throw;
}
finally
{
_timer.Start();
}
}
}
最后,应用设置
"NLog": {
"internalLogLevel": "Debug","internalLogFile": "c:\\temp\\internal-nlog.txt","extensions": {
"NLog.Extenstions.Logging": {
"assembly": "Nlog.Extensions.Logging"
}
},"targets": {
"logconsole": {
"type": "Console","layout": "${longdate}|${level}|${message} |${all-event-properties} ${exception:format=tostring}"
},"allfile": {
"type": "File","fileName": "c:\\temp\\nlog-all-${shortdate}.log","layout": "${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"
}
},"rules": [
{
"logger": "*","minLevel": "Debug","writeto": "logconsole"
},{
"logger": "*","minLevel": "Trace","writeto": "allfile"
}
]
}
我注入的类有一个记录器的属性,并且按照 NLog 和我阅读过的文档中的说明进行注入。
private readonly ILogger<ClassName> _logger;
public ClassName(ILogger<ClassName> logger)
{
_logger = logger;
}
public async Task Process()
{
_logger.LogDebug("Process started");
//stuff being done here
}
但我似乎无法将结果记录到控制台或文件中。
我确保我的 AppSettings 设置为始终复制。
让我印象深刻的一件事是,在将内部 NLog 记录器设置为调试后,我注意到了这个条目,但它的本质有点超出我的理解,而且我似乎无法在网上找到类似的问题:
021-02-17 12:20:18.4969 未为记录器配置调试目标: Topshelf.HostFactory 2021-02-17 12:20:18.4969 调试目标不 为记录器配置:Topshelf.HostConfigurators.HostConfiguratorImpl 2021-02-17 12:20:18.4969 未为记录器配置调试目标: Topshelf.Runtime.Windows.WindowsHostEnvironment 2021-02-17 12:20:18.6532 未为记录器配置调试目标: Topshelf.Builders.RunBuilder 2021-02-17 12:20:18.6532 调试目标 未为记录器配置:Topshelf.Hosts.ConsoleRunHost
否则,NLog 文件中没有其他明显的问题将我指向该问题。这里有什么问题?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)