Created
January 28, 2021 16:28
-
-
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.
This file contains hidden or 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
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>(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
using https://github.com/adams85/filelogger