Im using dotnet worker service (.net 5) I integrated serilog as well as enrichers and sinks. But for some reason I dont see any outputs into my files logs.
Here is my appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=.;Database=eeee;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"WorkerOptions": {
"Batch": 5
},
"Serilog": {
"Using": [],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
{ "Name": "Console" },
{
"Name": "File",
"Args": {
"path": "E:\\Logs\\log_.txt",
"outputTemplate": "{Timestamp:o} {Message}{NewLine:1}{Exception:1}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
},
{
"Name": "File",
"Args": {
"path": "E:\\Logs\\log_.json",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
"outputTemplate": "{Timestamp:o} {Message}{NewLine:1}{Exception:1}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
]
}
}
Registration of logger:
public class Program
{
public static void Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
services.AddScoped<ApplicationDbContext>(s => new ApplicationDbContext(optionsBuilder.Options));
services.AddHostedService<Worker>();
});
}
and this is an implementation:
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly IConfiguration _config;
public Worker(ILogger<Worker> logger, IServiceScopeFactory serviceScopeFactory, IConfiguration config)
{
_config = config;
_logger = logger;
_serviceScopeFactory = serviceScopeFactory;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Worker picked up {0} requests for dispatch at at: {time}", generatedRequests.Count(), DateTimeOffset.Now);
}
And when I run the worker through Kestrel I can see in my console logs, but not in my .txt file nor .json file
UPDATE #1 I tried with this answer below, but I still have an issue when I deploy my worker, I do not see any logs. When I run it thrpugh service.exe fi;le or just do debugg (Kestrel) Logs files are generated.
Here is updated application.json:
"Serilog": {
"Using": [],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
{ "Name": "Console" },
{
"Name": "File",
"Args": {
"path": ".\\Logs\\log_.txt",
"outputTemplate": "{Timestamp:o} {Message}{NewLine:1}{Exception:1}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
},
{
"Name": "File",
"Args": {
"path": ".\\Logs\\log_.json",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
"outputTemplate": "{Timestamp:o} {Message}{NewLine:1}{Exception:1}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
]
}
and this is the Program.cs
public class Program
{
public static void Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices((hostContext, services) =>
{
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseSqlServer(hostContext.Configuration.GetConnectionString("DefaultConnection"));
services.AddScoped<ApplicationDbContext>(s => new ApplicationDbContext(optionsBuilder.Options));
services.AddHostedService<Worker>();
})
.UseSerilog(); // no args
}
UPDATE#2 I updated Program class like this:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices((hostContext, services) =>
{
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseSqlServer(hostContext.Configuration.GetConnectionString("DefaultConnection"));
services.AddScoped<ApplicationDbContext>(s => new ApplicationDbContext(optionsBuilder.Options));
services.AddHostedService<Worker>();
})
.UseSerilog((hostContext, services, logger) => {
logger.ReadFrom.Configuration(hostContext.Configuration);
});
}
}
Also I put simple stuff in appsettings.json:
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
{ "Name": "Console" },
{
"Name": "File",
"Args": { "path": "Logs/log.txt" }
},
//{
// "Name": "File",
// "Args": {
// "path": ".\\Logs\\log_.txt",
// "outputTemplate": "{Timestamp:o} {Message}{NewLine:1}{Exception:1}",
// "rollingInterval": "Day",
// "retainedFileCountLimit": 7
// }
//},
{
"Name": "File",
"Args": {
"path": ".\\Logs\\log_.json",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
"outputTemplate": "{Timestamp:o} {Message}{NewLine:1}{Exception:1}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
]
}
Here is the also call of the _logger which is working, agaion when I start .exe file (console)
using Microsoft.Extensions.Logging;
...
private readonly ILogger<Worker> _logger;
...
_logger.LogInformation("Worker picked up {0} requests for dispatch at: {time}", generatedRequests.Count(), DateTimeOffset.Now);
UPDATE#3
I just setup absolute path for logging c:\\Logs\log.txt and it works partially - it creates a txt file, but file is empty.
Here is the new appsettings.json:
{
"ConnectionStrings": {
"DefaultConnection": "Server=.;Database=svcProcessor_dev;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"WorkerOptions": {
"Batch": 10,
"Delay": 5000,
"MaxAttempts": 3
},
"Serilog": {
"Using": ["Serilog.Sinks.Console", "Serilog.Sinks.File"],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
{ "Name": "Console" },
{
"Name": "File",
"Args": { "path": "C:\\Logs\\log.txt" }
}
//{
// "Name": "File",
// "Args": {
// "path": "C:\\Logs\\log_.txt",
// "outputTemplate": "{Timestamp:o} {Message}{NewLine:1}{Exception:1}",
// "rollingInterval": "Day",
// "retainedFileCountLimit": 7
// }
//}
//{
// "Name": "File",
// "Args": {
// "path": "C:\\Logs\\log_.json",
// "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
// "outputTemplate": "{Timestamp:o} {Message}{NewLine:1}{Exception:1}",
// "rollingInterval": "Day",
// "retainedFileCountLimit": 7
// }
//}
]
}
}
update#4
Here is the minimal example of my worker worker example