In .NET, I have following code
var logger = new LoggerConfiguration()
.Enrich.WithProperty("Assembly", assembly?.Name)
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Debug)
.WriteTo.Console(
theme: Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme.Code,
formatProvider: CultureInfo.CurrentCulture,
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}][{Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}",
restrictedToMinimumLevel: LogEventLevel.Debug)
.WriteTo.File(
path: "Logs/log-.log",
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}][{Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}",
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: LogEventLevel.Debug,
formatProvider: CultureInfo.CurrentCulture)
.WriteTo.Sink(new WebSocketSink(logChannel), LogEventLevel.Debug)
.WriteTo.Seq("http://localhost:5341")
.CreateLogger();
When I pass this logger as argument to other classes where ILogger is used of Microsoft,
the logging statements doesn't appear on console or in file.
But when I change it to
Log.logger = new LoggerConfiguration()..
logging statements starts appearing on console and in file as well.
Here is excerpt of class that I am using ILogger as argument
public class MyService(IDeviceIO devIO, ILogger<MyService> logger) : IMyService
{
/// <inheritdoc/>
public void OpenDevice(string host)
{
logger.LogInformation("Device is being opened!");
devIO.Open(host);
}
...
Why can't I use var logger = ..
? Do I need to use Log.logger
which static instance always ?
I found out the problem. Added only three lines as follows and solved my problem
var logger = new LoggerConfiguration()
.Enrich.WithProperty("Assembly", assembly?.Name)
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Debug)
.WriteTo.Console(
theme: Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme.Code,
formatProvider: CultureInfo.CurrentCulture,
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}][{Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}",
restrictedToMinimumLevel: LogEventLevel.Debug)
.WriteTo.File(
path: "Logs/log-.log",
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}][{Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}",
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: LogEventLevel.Debug,
formatProvider: CultureInfo.CurrentCulture)
.WriteTo.Sink(new WebSocketSink(logChannel), LogEventLevel.Debug)
.WriteTo.Seq("http://localhost:5341")
.CreateLogger();
builder.Host.UseSerilog(logger);
builder.Services.AddLogging();
Thanks for help ! It was DI issue in short !