Scenario: ASP.NET MVC 6 web application.
Here are the package imported in project.json
:
"Serilog": "2.3.0",
"Serilog.Extensions.Logging": "1.3.1",
"Serilog.Sinks.RollingFile": "3.2.0",
"Serilog.Settings.Configuration": "2.2.0"
Here is appsettings
configuration
"Serilog": {
"Using": [ "Serilog.Sinks.RollingFile" ],
"MinimumLevel": {
"Default": "Verbose",
"Override": {
"Microsoft": "Warning",
"Microsoft.AspNetCore.Identity": "Debug",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"path": "logger-{Date}.log",
"outputTemplate": "{Timestamp:o} [{Level:u3}] ({Application}/{MachineName}/{ThreadId}) {Message}{NewLine}{Exception}",
"fileSizeLimitBytes": 16384
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Properties": {
"Application": "Sample"
}
},
Here is Startup
constructor
var builder = new ConfigurationBuilder()
.SetBasePath( _environment.ContentRootPath )
.AddJsonFile( "appsettings.json", optional: true, reloadOnChange: true )
.AddJsonFile( $"appsettings.{_environment.EnvironmentName}.json", optional: true );
Configuration = builder.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration( Configuration )
.CreateLogger();
In configure method I have
loggerFactory.AddConsole();
loggerFactory.AddSerilog();
Where is supposed to be written the file? Can be a problem related to write permission?
EDIT
Followed @Sanket suggestions but still does not work. I have also switched to kestrel to see if it was working on the console but still no luck. Here's what I see:
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:5000/
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Executing action method MyCompany.MyProject.Host.Controllers.HomeController.Index MyCompany.MyProject.Host) with arguments (
(null)) - ModelState is Valid
info: Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor[1]
Executing ViewResult, running view at path /Views/Home/Index.cshtml.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action MyCompany.MyProject.Host.Controllers.HomeController.Index (MyCompany.MyProject.Host) in 2614.9118ms
This is very strange to me as in the Home Controller in Index action I just have
public IActionResult Index() {
_logger.LogDebug( "Index in HomeController called!" );
return View();
}
In startup, you can specify path of rolling file like this-
UPDATE:
In appsettings.json, change path argument to pathFormat
to
and file will be created at project location.
Refer this link
UPDATE2:
In Startup constructor, change serilog logger like this-
From
To