Skip to content

Instantly share code, notes, and snippets.

@maryamariyan
Last active July 18, 2020 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maryamariyan/81f1526fe2156e95352e516f03a61724 to your computer and use it in GitHub Desktop.
Save maryamariyan/81f1526fe2156e95352e516f03a61724 to your computer and use it in GitHub Desktop.
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());
}
}
}
// 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