Lor*_*nzo 3 logging serilog asp.net-core-mvc asp.net-core
场景:ASP.NET MVC 6 Web 应用程序。
这是导入的包project.json:
"Serilog": "2.3.0",
"Serilog.Extensions.Logging": "1.3.1",
"Serilog.Sinks.RollingFile": "3.2.0",
"Serilog.Settings.Configuration": "2.2.0"
Run Code Online (Sandbox Code Playgroud)
这里是appsettings配置
"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"
}
},
Run Code Online (Sandbox Code Playgroud)
这是Startup构造函数
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();
Run Code Online (Sandbox Code Playgroud)
在配置方法中我有
loggerFactory.AddConsole();
loggerFactory.AddSerilog();
Run Code Online (Sandbox Code Playgroud)
文件应该写在哪里?可能是与写权限有关的问题吗?
编辑
遵循@Sanket 的建议,但仍然无效。我也切换到 kestrel,看看它是否在控制台上工作,但仍然没有运气。这是我所看到的:
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
Run Code Online (Sandbox Code Playgroud)
这对我来说很奇怪,因为在我刚刚拥有的 Index action 中的 Home Controller 中
public IActionResult Index() {
_logger.LogDebug( "Index in HomeController called!" );
return View();
}
Run Code Online (Sandbox Code Playgroud)
在启动时,您可以像这样指定滚动文件的路径-
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.RollingFile(Path.Combine(env.ContentRootPath, "Serilog-{Date}.txt"))
.CreateLogger();
Run Code Online (Sandbox Code Playgroud)
更新:
在 appsettings.json 中,将路径参数更改为pathFormat
"path": "logger-{Date}.log",
Run Code Online (Sandbox Code Playgroud)
到
"pathFormat": "logger-{Date}.log",
Run Code Online (Sandbox Code Playgroud)
和文件将在项目位置创建。
参考这个链接
更新2:
在启动构造函数中,像这样更改 serilog 记录器 -
从
var logger = new LoggerConfiguration()
.ReadFrom.Configuration( Configuration )
.CreateLogger();
Run Code Online (Sandbox Code Playgroud)
到
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.CreateLogger();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5749 次 |
| 最近记录: |