Serilog 无法显示输出日志 aspnetcore 2.1 vs 2017
Serilog cannot show output logs aspnetcore 2.1 vs 2017
我正在使用 aspnetcore core 2.1 编写一些 webapi
我想使用 serilog,但我找不到一个简单的例子可以给我一个起点并告诉我我需要做什么。
我想做以下事情
- 在 appsetting.json 文件中设置 Serilog
- 写入 VisualStudio 输出 windows
- 写入文件位置例如"C:\Logs"
在各种控制器中注入记录器
有人可以更正我的代码或将我指向一个 link 它显示了它是如何使用的
我做了如下操作:(从互联网复制和粘贴)但它们没有显示在 vs 输出中 windows
不确定是否需要所有这些 nugets
- Serilog
- Serilog.Extensions.Logging
- Serilog.Settings.Configuration
- Serilog.Sinks.ColoredConsole
- Serilog.Sinks.File
- Serilog.Sinks.RollingFile
- serilog.Sinks.Trace
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.Enrich.FromLogContext()
.WriteTo.ColoredConsole()
.WriteTo.Console()
.CreateLogger();
Log.Verbose("This is a verbose statement");
Log.Debug("This is a debug statement");
Log.Information("This is a info statement");
Log.Warning("This is a warning statement");
Log.Error(new IndexOutOfRangeException(), "This is an error statement");
Log.Fatal(new AggregateException(), "This is an fatal statement");
Appsetting 文件设置 serilog
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "[ColoredConsole]",
"Args": {"outputTemplate": "[{Timestamp:HH:mm:ss.fff}] {Level:u3} - {Message}{NewLine}{Exception}"}
},
"WriteTo": [
{
"Name": "console",
"Args": {"outputTemplate": "[{Timestamp:HH:mm:ss.fff}] {Level:u3} - {Message}{NewLine}{Exception}"}
},
{
"Name": "RollingFile",
"Args": {
"pathFormat": "logs\log-{Date}.log",
"outputTemplate": "[{Timestamp:dd/MM/yy HH:mm:ss.fff z}] {Level:u3} {Message}{NewLine}{Exception}"
}
}
]
},
- VS OUTPUT 中没有任何显示 WINDOWS
- 没有文件被写入
我哪里错了?
谢谢
您始终可以从复制找到的示例应用程序开始 here。它将帮助您完成部分工作,然后您需要做的就是添加您需要的特定接收器。
我正在使用 aspnetcore core 2.1 编写一些 webapi 我想使用 serilog,但我找不到一个简单的例子可以给我一个起点并告诉我我需要做什么。
我想做以下事情
- 在 appsetting.json 文件中设置 Serilog
- 写入 VisualStudio 输出 windows
- 写入文件位置例如"C:\Logs"
在各种控制器中注入记录器
有人可以更正我的代码或将我指向一个 link 它显示了它是如何使用的
我做了如下操作:(从互联网复制和粘贴)但它们没有显示在 vs 输出中 windows
不确定是否需要所有这些 nugets
- Serilog
- Serilog.Extensions.Logging
- Serilog.Settings.Configuration
- Serilog.Sinks.ColoredConsole
- Serilog.Sinks.File
- Serilog.Sinks.RollingFile
- serilog.Sinks.Trace
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.Enrich.FromLogContext()
.WriteTo.ColoredConsole()
.WriteTo.Console()
.CreateLogger();
Log.Verbose("This is a verbose statement");
Log.Debug("This is a debug statement");
Log.Information("This is a info statement");
Log.Warning("This is a warning statement");
Log.Error(new IndexOutOfRangeException(), "This is an error statement");
Log.Fatal(new AggregateException(), "This is an fatal statement");
Appsetting 文件设置 serilog
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "[ColoredConsole]",
"Args": {"outputTemplate": "[{Timestamp:HH:mm:ss.fff}] {Level:u3} - {Message}{NewLine}{Exception}"}
},
"WriteTo": [
{
"Name": "console",
"Args": {"outputTemplate": "[{Timestamp:HH:mm:ss.fff}] {Level:u3} - {Message}{NewLine}{Exception}"}
},
{
"Name": "RollingFile",
"Args": {
"pathFormat": "logs\log-{Date}.log",
"outputTemplate": "[{Timestamp:dd/MM/yy HH:mm:ss.fff z}] {Level:u3} {Message}{NewLine}{Exception}"
}
}
]
},
- VS OUTPUT 中没有任何显示 WINDOWS
- 没有文件被写入
我哪里错了?
谢谢
您始终可以从复制找到的示例应用程序开始 here。它将帮助您完成部分工作,然后您需要做的就是添加您需要的特定接收器。