Skip to content

Instantly share code, notes, and snippets.

@afruzan
Created January 28, 2021 16:28
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 afruzan/1ac375e9fe215c6f7f2649b4007ea9fa to your computer and use it in GitHub Desktop.
Save afruzan/1ac375e9fe215c6f7f2649b4007ea9fa to your computer and use it in GitHub Desktop.
asp.net core (tested on 5) sample for simple and light and also powerful file logger provider.
public class Program
{
private static DateTime _startupTimestamp;
public static void Main(string[] args)
{
_startupTimestamp = DateTime.Now;
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureLogging((ctx, logBuilder) =>
{
logBuilder.AddConfiguration(ctx.Configuration.GetSection("Logging"));
// enable Karambolo.Extensions.Logging.File (https://github.com/adams85/filelogger)
logBuilder.AddFile(o =>
{
o.RootPath = ctx.HostingEnvironment.ContentRootPath;
o.PathPlaceholderResolver = (placeholderName, inlineFormat, context) => placeholderName switch
{
"date" => context.FormatDate(inlineFormat),
"counter" => (context.Counter + 1).ToString(inlineFormat ?? context.CounterFormat, CultureInfo.InvariantCulture) /*context.FormatCounter(inlineFormat)*/,
"timestamp" => _startupTimestamp.ToString("yyyyMMddHHmmssFFF" /*inlineFormat ?? context.DateFormat*/, CultureInfo.InvariantCulture),
_ => null,
};
});
});
webBuilder.UseStartup<Startup>();
});
}
@afruzan
Copy link
Author

afruzan commented Jan 28, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment