【发布时间】:2019-10-09 16:26:39
【问题描述】:
按照答案: .NET Core 3 Worker Service Settings Dependency Injection
我可以在 Worker.cs 类的 Debug 或 Release 中获取设置 但是当部署为 Windows 服务时,此值返回为 null
Worker.cs
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly ServiceSettings _serviceSettings;
private readonly ServiceConfigurations _serviceConfigurations;
public Worker(ILogger<Worker> logger, IOptions<ServiceConfigurations> serviceConfigurations, IOptions<ServiceSettings> serviceSettings)
{
_logger = logger;
_serviceConfigurations = serviceConfigurations.Value;
_logger.LogInformation($"Worker running at: {DateTime.Now}");
_serviceSettings = serviceSettings.Value;
string retornoPathLog = null;
string PathLog = _serviceSettings.PathLog;
if (!Directory.Exists(PathLog))
{
retornoPathLog = "Diretório de LOG " + PathLog;
Directory.CreateDirectory(PathLog);
}
//Configure Serilo for Logging
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.Enrich.FromLogContext()
.WriteTo.File(PathLog + "log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
if (string.IsNullOrEmpty(retornoPathLog) == false)
{
Log.Information("[WFoneService - Watch Event] " + retornoPathLog);
}
}
}
appsettings.json:
{
"ConnectionStrings": {
"WFoneConnection": ""
},
"ServiceConfigurations": {
"UrlSignalrNotification": "urlValue",
"WatchIp": "ipValue",
"WatchPort": "portValue"
},
"ServiceSettings": {
"PathLog": "C:\\Log\\"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
【问题讨论】:
-
显示windows服务的启动代码。
标签: c# asp.net-core