Last active
July 18, 2020 00:19
-
-
Save maryamariyan/81f1526fe2156e95352e516f03a61724 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ConsoleApp46 | |
{ | |
class Program | |
{ | |
public static async Task Main(string[] args) | |
{ | |
using var loggerFactory = LoggerFactory.Create(builder => | |
{ | |
builder | |
// current usage which is to be changed: | |
.AddConsole(o => | |
{ | |
//o.Format = ConsoleLoggerFormat.Default; <--- deprecated | |
//o.IncludeScopes = false; <--- deprecated | |
//o.UseUtcTimestamp = false; <--- deprecated | |
//o.TimestampFormat = "HH:mm:ss"; <--- deprecated | |
//o.DisableColors = true; <--- deprecated | |
o.LogToStandardErrorThreshold = LogLevel.Trace; | |
}); | |
}); | |
var logger = loggerFactory.CreateLogger<Program>(); | |
logger.Log(LogLevel.Information, 0, new FormattedLogValues("This log contains {0}", "this parameter"), null, (state, error) => state.ToString()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// below shows different usages for setting up console logging: | |
builder.AddConsole(); | |
builder.AddConsole(o => | |
{ | |
o.FormatterName = ConsoleLogFormatterNames.Default; | |
o.LogToStandardErrorThreshold = LogLevel.Trace; | |
}; | |
); | |
// Default Formatter, o is DefaultConsoleLogFormatterOptions | |
builder.AddSimpleConsole(o => | |
{ | |
o.IncludeScopes = false; | |
o.UseUtcTimestamp = false; | |
o.TimestampFormat = "HH:mm:ss"; | |
o.SingleLine= false; | |
o.DisableColors = false; | |
}); | |
// Adds a readable and colored compact format which will log in a single line: | |
builder.AddSimpleConsole(o => | |
{ | |
o.SingleLine= true; | |
}); | |
// Adds a JSON based log format. | |
builder.AddJsonConsole(o => | |
{ | |
o.IncludeScopes = false; | |
o.UseUtcTimestamp = false; | |
o.TimestampFormat = "HH:mm:ss"; | |
o.JsonWriterOptions = new JsonWriterOptions() | |
{ | |
Indented = true | |
}; | |
}) | |
// Systemd Formatter, o is ConsoleLogFormatterOptions | |
builder.AddSystemdConsole(o => | |
{ | |
o.IncludeScopes = false; | |
o.UseUtcTimestamp = false; | |
o.TimestampFormat = "HH:mm:ss"; | |
}); | |
// Adds an extension point so that users can specify their own custom log formatters. | |
builder | |
.AddConsoleLogFormatter<CustomConsoleLogFormatter, CustomConsoleLogFormatterOptions>((o) => | |
{ | |
o.IncludeScopes = true; | |
o.ExtraCustomProperty = "property on custom formatter"; | |
}) | |
.AddConsoleLogFormatter<CustomJsonFormatter, CustomJsonFormatterOptions>((o) => | |
{ | |
o.IncludeScopes = true; | |
o.JsonWriterOptions = new JsonWriterOptions() | |
{ | |
Indented = true | |
}; | |
}) | |
.AddConsole(o => | |
{ | |
o.FormatterName = "customJson"; | |
o.LogToStandardErrorThreshold = LogLevel.Trace; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment