I want to change/override the filename declared appsettings.json with whatever I provide when I setup the logger while keeping all the other settings as is.
I have serilog settings declared in the appsettings.json
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Information",
"System": "Information"
}
},
"WriteTo": [
{ "Name": "Console" },
{
"Name": "File",
"Args": {
"path":"MyAppName_.log"
"rollOnFileSizeLimit": "true",
"fileSizeLimitBytes": 1000000,
"rollingInterval": "Day",
"flushToDiskInterval": 1,
"outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message:lj}{NewLine}{Exception}"
}
}
],
"Enrich": [ "FromLogContext", "%COMPUTERNAME%", "WithThreadId", "USERNAME","USERDOMAIN" ],
"Properties": { "Application": "MyAppName" }
}
The below code is in ConfigureServices method in Startup.cs
var appName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
var fileName = #$"C://Logs/{MyAppName}\{MyAppName}-{DateTime.Today.ToString("MM-dd-yyyy")}.log";
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.WriteTo.File(fileName)
.CreateLogger();
Is it possible?
I am using ASP.NET Core 3.1 and Serilog.AspNetCore 3.2.0
Related
I have setup Serilog with .net 6 asp.net core project but the IP Address is not showing up in the logs using the json formatter.
var logger = new LoggerConfiguration()
.Enrich.WithClientIp()
.Enrich.WithClientAgent()
.Enrich.FromLogContext()
.ReadFrom.Configuration(builder.Configuration)
.CreateLogger();
builder.Logging.ClearProviders();
builder.Logging.AddSerilog(logger);
And I have setup my appsettings like this. But I'm not sure why the IP Address is not showing up in the logs for any request (get, put, etc.)
"Serilog": {
"Using": [ "Serilog.Sinks.File, Serilog.Enrichers.ClientInfo" ],
"MinimumLevel": {
"Default": "Information"
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId", "WithClientIP", "WithClientAgent" ],
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "===> {Timestamp:HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"path": "C://Temp//appLog_.json",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
"rollOnFileSizeLimit": true,
"fileSizeLimitBytes": 4194304,
"retainedFileCountLimit": 10,
"rollingInterval": "Day"
}
}
]
},
In Program.cs, I change the code into below code:
builder.Host.UseSerilog((ctx, lc) => lc
.WriteTo.Console()
.ReadFrom.Configuration(ctx.Configuration));
...
app.UseSerilogRequestLogging(options =>
{
// Customize the message template
options.MessageTemplate = "{RemoteIpAddress} {RequestScheme} {RequestHost} {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms";
// Emit debug-level events instead of the defaults
options.GetLevel = (httpContext, elapsed, ex) => LogEventLevel.Debug;
// Attach additional properties to the request completion event
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
{
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
diagnosticContext.Set("RemoteIpAddress", httpContext.Connection.RemoteIpAddress);
};
});
Read net6.0 Serilog example and Adding IP logging to know more.
I have an Asp.net core web api using .net core 6.
I use Serilog to store the app log in a text file.
sometimes I get this error:
Caught exception while emitting to sink Serilog.Sinks.SystemConsole.ConsoleSink: System.IO.IOException: The handle is invalid.
at System.ConsolePal.WindowsConsoleStream.Write(ReadOnlySpan`1 buffer)
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
at System.IO.StreamWriter.Write(String value)
at System.IO.TextWriter.SyncTextWriter.Write(String value)
at Serilog.Sinks.SystemConsole.ConsoleSink.Emit(LogEvent logEvent)
at Serilog.Core.Sinks.SafeAggregateSink.Emit(LogEvent logEvent)
could you please help me how can I solve this issue?
this is my appsetting:
"Serilog": {
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.File"
],
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "AppLogs/bckLog-.log",
"rollingInterval": "Day",
"restrictedToMinimumLevel": "Warning"
}
},
{
"Name": "Console"
}
],
"Enrich": [
"FromLogContext"
]
}
and this is my program.cs
Serilog.Debugging.SelfLog.Enable(message => { Log.Logger?.Error(message); Console.WriteLine(message); });
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).CreateLogger();
builder.Host.UseSerilog();
I'm using the SQL Server Serilog sink and using different appsettings.<EVN>.json configuration files (one for the default which is appsettings.json and one for the specific environment I am running in which is currently appsettings.test.json). appsettings.json specifies a SQL Server localhost in the connection string and the appsettings.test.json file specifies a different SQL server for the connection string. When running under any environment other than DEVELOPMENT, Serilog will create a LOG table in both the localdb and whatever environment I am testing (TEST_MSSQL in this case). My assumption is that there would only be one log configured and that the server setting in the appsettings.test.json (External SQL Server) would override the value in the default appsettings.json (Local SQL Server), but it appears to be creating configuring in both the localhost and test SQL Servers.
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog((context, config) => config.ReadFrom.Configuration(Configuration))
.Build()
.Run();
Settings in default appsettings.json
"Serilog": {
"Using": [ "Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Error"
}
},
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyDB;Integrated Security=True;Connect Timeout=30;",
"schemaName": "dbo",
"tableName": "Log",
"autoCreateSqlTable": true,
"restrictedToMinimumLevel": "Warning",
"batchPostingLimit": 1000,
"period": "0.00:00:30",
"columnOptionsSection": {
"addStandardColumns": [ "LogEvent" ],
"removeStandardColumns": [ "MessageTemplate", "Properties" ],
"logEvent": {
"excludeAdditionalProperties": true,
"excludeStandardColumns": true
}
}
}
}
]
}
Settings in appsettings.test.json
"Serilog": {
"WriteTo": [
{
"Args": {
"connectionString": "Data Source=TEST_MSSQL;Initial Catalog=MyDB;Integrated Security=True;Connect Timeout=30;"
}
}
]
}
When running under any environment other than DEVELOPMENT, Serilog will create a LOG table in both the localdb and whatever environment I am testing (TEST_MSSQL in this case).
I can reproduce same issue based on the configuration data you provided.
To override default connectionString for Serilog you specified in appsettings.json file and fix above issue, you can update your appsettings.test.json file like below.
{
"Serilog": {
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Data Source=TEST_MSSQL;Initial Catalog=MyDB;Integrated Security=True;Connect Timeout=30;",
}
}
]
}
}
Logging to AzureTableStorage is causing an overhead that blocks next calls, compared to when logging into a file, is there a way to make this process faster (batch or async option)?
My code in appsetttings.josn:
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "AzureTableStorage",
"Args": {
"storageTableName": "tbl",
"connectionString": "DefaultEndpointsProtocol=http;AccountName=xxx;AccountKey=/xxxxxxxx"
}
}
]
}
I ended up using https://github.com/serilog/serilog-sinks-async and the batch option in AzureTableStorage, similar to:
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "AzureTableStorage",
"Args": {
"storageTableName": "tbl",
"connectionString": "DefaultEndpointsProtocol=http;AccountName=xxx;AccountKey=xxx"
}
}
]
}
}
]
This is very similar to this question except its about appending arrays between two different JSON files.
I have an ASP.NET Core application
I have the following in appsettings.Development.Json
"Serilog": {
"WriteTo": [
{
"Name": "ApplicationInsightsTraces",
"Args": { "instrumentationKey": "XXXXXXXX" }
}
]
}
And in appsettings.json:
"Serilog": {
// . . . Rest of Serilog configs
"WriteTo": [
{
"Name": "Console",
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {RequestId}-{SourceContext} {$Scope:lj}: {Message:lj}{NewLine}{Exception}"
},
"restrictedToMinimumLevel": "Information"
},
}
Because Keys overwrite other keys in Appsettings.json I end up with the Console sink being overridden. Is there a syntax to allow it to be appended?
The answer is to use WriteTo:1 and make it an object not an array in appsettings.Development.json like so:
"Serilog": {
"WriteTo:1":
{
"Name": "ApplicationInsightsTraces",
"Args": { "instrumentationKey": "d95066c9-0b17-4e0a-84d4-bb2a4f111016" }
}
}