When starting up my .Net 6 Web Api project, in the program.cs I am using Two-stage initialisation from serilog-aspnetcore to initially output to console, and then in the second phase, output logs to a database.
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateBootstrapLogger();
// down from the above, the proper logging configuration
// add logging
builder.Host.UseSerilog(
(context, services, configuration) =>
configuration.ReadFrom
.Configuration(context.Configuration, "Serilog")
.ReadFrom.Services(services)
.Enrich.FromLogContext()
);
Inside my configuration I am calling
"autoCreateSqlTable": true,
The problem is, it is not until after I have run var app = builder.Build(); that I have initialised my database (migrating via EF).
// execute database initialiser to ensure database is current
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var initialiser = services.GetRequiredService<DbInitialiser>();
initialiser.Run();
}
This is causing errors as serilog is attempting to create the logging table before I have created the database.
Is there a way to delay transitioning to using the proper serilog configuration until after I have initialised the database successfully?
I could create the logging table via EF, but I would still be using the second stage config before the setup completes (i.e. I only want console logging until database logging occurs).
I have attempted running the two phase initialisation as per instructions and it is attempting to create the log table before ef initialisation.
I have attempted to manually create the logger after initialisation via:
Log.Logger = new LoggerConfiguration()
.ReadFrom
.Configuration(builder.Configuration, "Serilog")
.ReadFrom.Services(app.Services)
.Enrich.FromLogContext()
.CreateLogger();
But then no logs are created in the database.
Related
I currently have several .Net windows services running on my server. Is there a way to attach a console app to the service to get all of the ILogger data? I have had issues where the service runs perfectly as a console app/worker service but as soon as I run it as a windows service, it just sits there and does nothing.
I did find an article about attaching the VS debugger to the process, but this will not work with our network security.
I am open to any other suggestions as well.
The technical answer is no, but as #Fildor mentioned, you would set up a log sink of some sort. The file logger is just an example, but you can also have the logs send emails, post to some cloud logging service such as splunk or cloudwatch, etc.
One issue you may run into is that you need to capture an error prior to ILogger being available and properly configured for you. Here is a guide I followed for capturing startup errors using NLog: https://alistairevans.co.uk/2019/10/04/asp-net-core-3-0-logging-in-the-startup-class-with-nlog/
Startup classes are no longer necessary in the latest .NET version, so I modified their example to be code you would have in Program.cs:
// NLog: setup the nlog config first
NLogBuilder.ConfigureNLog("nlog.config");
try
{
var host = Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(LogLevel.Trace);
})
// Use NLog to provide ILogger instances.
.UseNLog()
.Build();
host.Run();
}
catch (Exception ex)
{
var logger = nlogLoggerProvider.CreateLogger(typeof(Program).FullName);
}
}
Here's the list of available log sinks you can configure in that nlog configuration file: https://nlog-project.org/config/
This same thing can be accomplished with other log providers you may already be using such as Serilog, Log4Net, etc.
I'd like to have custom logging in my code, which will be hosted in Azure.
I've found two extensions for logging providers, between which I am not sure what is the difference.
The two providers that I'm using in my program.cs:
builder.Logging.AddAzureWebAppDiagnostics();
builder.Logging.AddApplicationInsights();
I tried to comment either and in Azure's logs the trace is still the same. Any idea what is the difference between the two and which one should I stick to?
builder.Logging.AddAzureWebAppDiagnostics();
By using AddAzureWebAppDiagnostics Method, we can add an Azure Diagnostics logger and can get the diagnostic logs in Azure App Service.
we need to configure file logger options in Program.cs file.
Install the NuGet Package Microsoft.Extensions.Logging.
In Program.cs file, add the below lines of code to configure diagnostic logs.
using Microsoft.Extensions.Logging.AzureAppServices;
builder.Logging.AddAzureWebAppDiagnostics();
builder.Services.Configure<AzureFileLoggerOptions>(options =>
{
options.FileName = "Diagnostics-Logs";
options.FileSizeLimit = 50 * 1024;
options.RetainedFileCountLimit = 3;
});
Build and deploy the App to Azure App service.
In Azure App service => App Service Logs => enable Application logging (Filesystem).
After enabling the filesystem, In Kudu console logs are created in a new file under D:\home\LogFiles\Application path.
builder.Logging.AddApplicationInsights();
AddApplicationInsights() method is to configure Application Insights for Azuer App Service.
Install the NuGet package Microsoft.Extensions.Logging.ApplicationInsights.
AddApplicationInsights() requires LogLevel .It is not mentioned,by default it takes LogLevel.Warning.
In Program.cs file, add the below code.
using Microsoft.Extensions.Logging.ApplicationInsights;
builder.Logging.AddApplicationInsights();
Controller
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
_logger.LogTrace("Trace Log");
_logger.LogDebug("Debug Log");
_logger.LogInformation("Information Log");
_logger.LogWarning("Warning Log");
_logger.LogError("Error Log");
_logger.LogCritical("Critical Log");
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
Application Insights
References taken from AddAzureWebAppDiagnostics and AddApplicationInsights
I want to create serilog logger file using docker image. I am able to create a file in console app but not able to create using docker.
Here is my sample code,
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.File(fileName, rollingInterval: RollingInterval.Day)
.WriteTo.File(#"C:\log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
In short, the docker idea is to isolate the application from the host it is running on. Your app is trying to access the 'c:\log.txt' file which resides on host, not the container. You can write to the file inside the container by specifying the path like '/app/log.txt' (note the linux-style).
If you really need the host-side log file, the more complicated approaches are required - this SO answer may help:
Expose log file outside docker container
Application is in .net framework and using Serilog for logging.
Configuration
Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.WriteTo.Console()
.CreateLogger()
In Unit test how to verify that logger has the values which is expected to be?
Is there any way to read the values using any of Serilog api/methods?
I'm trying to implement Serilog in a .Net Core library to have a good abstraction for this Third party and be able to use it on different project that are in my Solution.
So I configure Serilog like the example in their GitHub
if(Log.Logger == null){
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.LiterateConsole()
.WriteTo.RollingFile("logs/myUsefullLogs.txt")
.CreateLogger();
}
And I log an information like this :
Log.Information(message,ex,source);
Log.CloseAndFlush();
If I put a breakpoint all seems to work perfectly but when I search the file I'm not able to find it.
Somebody already face to this in macOS?
Log.Logger will never have the value null, so your configuration code above will never run.
When logging is not configured, Log.Logger is assigned an instance of the (internal) SilentLogger class: https://github.com/serilog/serilog/blob/dev/src/Serilog/Log.cs#L43.