我今天遇到了Serilog.Sinks.Map插件,这将解决我在将特定日志事件路由到特定接收器接口时遇到的挑战。在我的环境中,我正在写入日志文件以及使用SQL接口。不过,我只希望将某些日志写入Server。
阅读作者在GitHub上的说明,我只能看到一个在Program.CS中通过C#实现LoggerConfiguration的示例,但我使用的是appsettings.json文件,不知道如何将提供的示例更改为所需的json格式。
Serilog在GitHub上给出的示例:
Log.Logger = new LoggerConfiguration()
.WriteTo.Map("Name", "Other", (name, wt) => wt.File($"./logs/log-{name}.txt"))
.CreateLogger();
我的当前配置:注意,我还没有在代码中实现Sinks.Map。Program.CS文件:
public static void Main(string[] args)
{
// Build a configuration system with the route of the app settings.json file.
// this is becuase we dont yet have dependancy injection available, that comes later.
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
var host = CreateHostBuilder(args).Build();
}
这是我的appsettings.json文件。我希望能够将接收器名“MSSqlServer”配置为特殊的路由,然后使用标准文件附录接收器进行所有其他常规日志记录。
"AllowedHosts": "*",
"Serilog": {
"Using": [],
"MinumumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
{ "Name": "Console" },
{
"Name": "File",
"Args": {
//"path": "C:\\NetCoreLogs\\log.txt", // Example path to Windows Drive.
"path": ".\\Logs\\logs.txt",
//"rollingInterval": "Day", // Not currently in use.
"rollOnFileSizeLimit": true,
//"retainedFileCountLimit": null, // Not currently in use.
"fileSizeLimitBytes": 10000000,
"outputTemplate": "{Timestamp:dd-MM-yyyy HH:mm:ss.fff G} {Message}{NewLine:1}{Exception:1}"
// *Template Notes*
// Timestamp 'G' means UTC Time
}
},
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "DefaultConnection",
"schemaName": "EventLogging",
"tableName": "Logs",
"autoCreateSqlTable": true,
"restrictedToMinimumLevel": "Information",
"batchPostingLimit": 1000,
"period": "0.00:00:30"
}
}
//{
// "Name": "File",
// "Args": {
// "path": "C:\\NetCoreLogs\\log.json",
// "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
// }
//}
]
}
最后,如果我可以在这个主题上插入另一个快速问题,在使用SQL接收器接口时,如何管理最老事件的自动清除/删除,即DB应该只存储最大1,000,000个事件,然后首先自动写入最老的事件,谢谢
发布于 2020-08-10 12:18:50
我认为目前不可能在json中配置标准的Map
调用,因为它依赖于一些目前没有序列化支持的类型,比如Action<T1, T2>
。我创建了一个问题,以便在存储库本身中讨论这个问题:
但是,仍然有一种方法可以通过创建自定义扩展方法从Json中获得一些功能。在你的特殊情况下,它应该是这样的:
public static class SerilogSinkConfigurationExtensions
{
public static LoggerConfiguration MapToFile(
this LoggerSinkConfiguration loggerSinkConfiguration,
string keyPropertyName,
string pathFormat,
string defaultKey)
{
return loggerSinkConfiguration.Map(
keyPropertyName,
defaultKey,
(key, config) => config.File(string.Format(pathFormat, key));
}
}
然后,在json文件中添加如下节:
"WriteTo": [
...
{
"Name": "MapToFile",
"Args": {
"KeyPropertyName": "Name",
"DefaultKey": "Other",
"PathFormat": "./logs/log-{0}.txt"
}
}
]
为了使这些自定义正常工作,Serilog需要理解您的程序集具有这类扩展,以便在解析阶段加载它们。根据文档,您需要在*.Serilog.*
程序集中拥有这些扩展,或者在json上添加Using
子句:
// Assuming the extension method is inside the "Company.Domain.MyProject" dll
"Using": [ "Company.Domain.MyProject" ]
有关这些制约因素的更多信息如下:
https://stackoverflow.com/questions/59448546
复制相似问题