Sandy is a logger for .NET Core that will help you to log your request information in a json format into files.
##Installation
Install-Package Sandy -Pre
Inside the Startup class of your project, in the ConfigureServices method. inject the IJsonLogger as a Singleton
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddSingleton<IJsonLogger, JsonLogger>();
services.AddMvc();
}
Sandy comes with default options, however you can override these options
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory, IJsonLogger jsonLogger)
{
ConfigureJsonLogger(jsonLogger);
}
public void ConfigureJsonLogger(IJsonLogger jsonLogger)
{
jsonLogger.SetOptions(new JsonLoggerOptions()
{
AppName = "MyAppName",
});
}
You have to options in order for you to log
1- Injecting the IJsonLogger in the constructor and using the instance.
private IJsonLogger logger { get; set; }
public HomeController(IJsonLogger logger)
{
this.logger = logger;
}
public IActionResult()
{
logger.LogFatal(Request, "my message");
return View();
}
2- Using the HttpRequest extension methods.
public IActionResult()
{
Request.LogInfo();
Request.LogError("something went wrong");
}
- AppName (Default "DefaultApp")
- InfoFilePath (Default "C:/DefaultApp/Logs/DefaultApp-info.log").
- ErrorFilePath (Default "C:/DefaultApp/Logs/DefaultApp-error.log").
- DebugFilePath (Default "C:/DefaultApp/Logs/DefaultApp-debug.log").
- WarningFilePath (Default "C:/DefaultApp/Logs/DefaultApp-warning.log").
- FatalFilePath (Default "C:/DefaultApp/Logs/DefaultApp-fatal.log").
Because .NET Core still at its early stage, granting permissions for the files inside the code still not supported. so you might want to do it manually for now.
check my other logging repo here.
Feel free to drop me an email at ibraheem.al-saady@outlook.com