如何配置 `Serilog` 以使用配置文件写入应用程序目录?
How to config `Serilog` to write to the application directory with the cofig file?
我在 .net core
上使用 Serilog
。
我想配置应用程序目录的日志路径。
我看到有一个扩展程序 https://github.com/serilog/serilog-settings-configuration 可以让 Serilog
从 Configuration
读取数据。
在示例中,路径配置为"%TEMP%\Logs\serilog-configuration-sample.txt"
。
如何将其设置为工作目录?
我搜索了一下,知道可以通过代码来完成,但是似乎没有人问如何通过配置文件来做到这一点,即appsettings.json
。
当前配置:
{
"Serilog": {
"Using": [
"Serilog.Sinks.File"
],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "File",
"Args": { "path": "Logs\serilog-configuration-sample.txt" }
}
],
"Enrich": [ "FromLogContext", "WithMachineName" ],
"Destructure": [
],
"Properties": {
}
},
"AllowedHosts": "*"
}
我想将日志路径设置为工作目录。
但目前它在 "C:\Program Files\IIS Express".
您可以添加一个 "RollingFile" 可以写入本地路径文件。在这个例子中,我在项目根目录中的一个文件中写入,如下所示。
{
"Name": "RollingFile",
"Args": {
"pathFormat": ".\Logs\logs.txt",
"fileSizeLimitBytes": 1048576
}
},
appsettings.json 上的完整 json 也是这样结束的(如果你需要一个完整的例子)
...
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"System": "Debug",
"Microsoft": "Debug"
}
},
"WriteTo": [
{
"Name": "ApplicationInsightsEvents",
"Args": {
"instrumentationKey": "xxxxxxxxxx"
}
},
{
"Name": "RollingFile",
"Args": {
"pathFormat": ".\Logs\logs.txt",
"fileSizeLimitBytes": 1048576
}
},
{ "Name": "Console" },
{
"Name": "EventLog",
"Args": {
"source": "API NAME",
"logName": "CustomLog",
"restrictedToMinimumLevel": "Warning"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Properties": {
"Application": "API NAME"
}
}
...
像Logs/log.txt
这样的配置路径将在工作目录的logs
文件夹下写入日志文件
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/log.txt"
}
}
您也可以检查 其他选项
我在 .net core
上使用 Serilog
。
我想配置应用程序目录的日志路径。
我看到有一个扩展程序 https://github.com/serilog/serilog-settings-configuration 可以让 Serilog
从 Configuration
读取数据。
在示例中,路径配置为"%TEMP%\Logs\serilog-configuration-sample.txt"
。
如何将其设置为工作目录?
我搜索了一下,知道可以通过代码来完成,但是似乎没有人问如何通过配置文件来做到这一点,即appsettings.json
。
当前配置:
{
"Serilog": {
"Using": [
"Serilog.Sinks.File"
],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "File",
"Args": { "path": "Logs\serilog-configuration-sample.txt" }
}
],
"Enrich": [ "FromLogContext", "WithMachineName" ],
"Destructure": [
],
"Properties": {
}
},
"AllowedHosts": "*"
}
我想将日志路径设置为工作目录。 但目前它在 "C:\Program Files\IIS Express".
您可以添加一个 "RollingFile" 可以写入本地路径文件。在这个例子中,我在项目根目录中的一个文件中写入,如下所示。
{
"Name": "RollingFile",
"Args": {
"pathFormat": ".\Logs\logs.txt",
"fileSizeLimitBytes": 1048576
}
},
appsettings.json 上的完整 json 也是这样结束的(如果你需要一个完整的例子)
...
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"System": "Debug",
"Microsoft": "Debug"
}
},
"WriteTo": [
{
"Name": "ApplicationInsightsEvents",
"Args": {
"instrumentationKey": "xxxxxxxxxx"
}
},
{
"Name": "RollingFile",
"Args": {
"pathFormat": ".\Logs\logs.txt",
"fileSizeLimitBytes": 1048576
}
},
{ "Name": "Console" },
{
"Name": "EventLog",
"Args": {
"source": "API NAME",
"logName": "CustomLog",
"restrictedToMinimumLevel": "Warning"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
"Properties": {
"Application": "API NAME"
}
}
...
像Logs/log.txt
这样的配置路径将在工作目录的logs
文件夹下写入日志文件
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/log.txt"
}
}
您也可以检查