- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Serilog
的新手,我正在尝试确定如何使用 log 将序列化的
和 json
发送到 console
leveldate time
字段。 structured data
下的文档中似乎没有任何信息。
这是我在 Startup.cs
中调用的代码:
private void LoggerLoop(ILogger<Startup> logger)
{
RabbitModel rb = new RabbitModel
{
Id = 1,
DeviceNum = 1,
DeviceName = "Device 1",
InputNum = 1,
InputName = "Input 1",
InputState = 1,
OnPhrase = "On",
OffPhrase = "Off",
When = "2020-01-01T22:45:00.1124303+00:00"
};
while (true)
{
logger.LogInformation("{@rb}", rb);
Thread.Sleep(1000);
}
}
这是我的输出:
[14:28:22 INF] {“Id”:1,“DeviceNum”:1,“DeviceName”:“设备 1”,“InputNum”:1,“InputName”:“输入 1” , "InputState": 1, "OnPhrase": "On", "OffPhrase": "Off", "When": "2020-01-01T22:45:00.1124303+00:00", "$type": "RabbitModel "
我确实注意到它添加了一个字段 $type
并且想知道是否可以将 [14:28:22 INF]
添加到 >json
?
最佳答案
根据 12 factor app ,应用程序应将所有日志写入 stdout
/stderr
。
然后您需要将所有日志收集在一起,并路由到一个或多个最终目的地以供查看(Elasticserach)。开源日志路由器(例如 FluentBit、Fluentd 和 Logplex)可用于此目的。
因此,该应用从不关心其日志的路由或存储。在 dotnet 应用程序中,您可以使用 Serilog 轻松实现它
假设我们在 appsettings.json
中有以下记录器设置"Logging": {
"OutputFormat": "console",
"MinimumLevel": "Information"
}
我们可以创建一个扩展方法
private static IWebHostBuilder CreateWebHostBuilder() =>
WebHost.CreateDefaultBuilder()
.UseStartup<Startup>()
.UseLogging();
}
可以以纯文本和elasticsearch格式将日志写入控制台。纯文本日志对开发很有用,因为它更易于阅读。在生产环境中,我们启用 elasticsearch 格式并仅在 Kibana 中查看所有日志。
带注释的扩展代码:
public static IWebHostBuilder UseLogging(this IWebHostBuilder webHostBuilder, string applicationName = null) =>
webHostBuilder
.UseSetting("suppressStatusMessages", "True") // disable startup logs
.UseSerilog((context, loggerConfiguration) =>
{
var logLevel = context.Configuration.GetValue<string>("Logging:MinimumLevel"); // read level from appsettings.json
if (!Enum.TryParse<LogEventLevel>(logLevel, true, out var level))
{
level = LogEventLevel.Information; // or set default value
}
// get application name from appsettings.json
applicationName = string.IsNullOrWhiteSpace(applicationName) ? context.Configuration.GetValue<string>("App:Name") : applicationName;
loggerConfiguration.Enrich
.FromLogContext()
.MinimumLevel.Is(level)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.Enrich.WithProperty("Environment", context.HostingEnvironment.EnvironmentName)
.Enrich.WithProperty("ApplicationName", applicationName);
// read other Serilog configuration
loggerConfiguration.ReadFrom.Configuration(context.Configuration);
// get output format from appsettings.json.
var outputFormat = context.Configuration.GetValue<string>("Logging:OutputFormat");
switch (outputFormat)
{
case "elasticsearch":
loggerConfiguration.WriteTo.Console(new ElasticsearchJsonFormatter());
break;
default:
loggerConfiguration.WriteTo.Console(
theme: AnsiConsoleTheme.Code,
outputTemplate: "[{Timestamp:yy-MM-dd HH:mm:ss.sssZ} {Level:u3}] {Message:lj} <s:{Environment}{Application}/{SourceContext}>{NewLine}{Exception}");
break;
}
});
当 OutputFormat
为 elasticsearch
时,日志将是这样的
{"@timestamp":"2020-02-07T16:02:03.4329033+02:00","level":"Information","messageTemplate":"Get customer by id: {CustomerId}","message":"Get customer by id: 20","fields":{"CustomerId":20,"SourceContext":"Customers.Api.Controllers.CustomerController","ActionId":"c9d77549-bb25-4f87-8ea8-576dc6aa1c57","ActionName":"Customers.Api.Controllers.CustomerController.Get (Customers.Api)","RequestId":"0HLTBQP5CQHLM:00000004","RequestPath":"/v1/customers","CorrelationId":"daef8849b662117e","ConnectionId":"0HLTBQP5CQHLM","Environment":"Development","ApplicationName":"API","Timestamp":"2020-02-07T14:02:03.4329033Z"}}
在其他情况下(仅用于调试)
[20-02-07 13:59:16.16Z INF] Get customer by id: 20
然后您应该配置日志路由器以从容器中收集日志并将其发送到 Elasticsearch。
如果所有日志都是structured ,它改进了 Kibana 中的搜索和创建索引。
关于c# - 如何使用 Serilog 在 Json 中获取日志级别 - .NET Core 3.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60059882/
我正在寻找一种为 serilog 提供独立 XML/JSON 配置文件的方法,该文件是从应用程序本身运行的目录中动态加载的。 我正在寻找类似的东西 NLog provides .如 Nlog#Conf
我在我的 aspnet core 应用程序中使用 Serilog 进行日志记录。我需要频繁地将日志事件写入控制台(每秒 300-500 个事件)。我在 docker 容器内运行我的应用程序,并使用 O
是否可以创建一个自动为每条消息添加前缀的记录器? 我通常在每条消息前加上帐户前缀,因为这样更容易阅读,像这样: user1 - did something user2 - did another th
我具有动态更改日志文件路径的功能。但是,当我更改 Consul 中可配置的路径时,它会在两个地方(即旧路径和新路径)写入部分日志。更改日志文件路径应该可以在没有任何服务重启的情况下工作。我们如何存档?
我有很多这样的日志: Log.Information("Submitting order {@order}", order); 此日志通过 RabbitMq -> LogStash -> Elasti
我有很多这样的日志: Log.Information("Submitting order {@order}", order); 此日志通过 RabbitMq -> LogStash -> Elasti
我在服务器端使用 Serilog 为我所有的 .NETCore 服务使用控制台、文件和 Graylog 接收器。我也喜欢在我的 Windows 胖客户端(WPF 应用程序)中使用它。 与 后者我有问题
我正在使用 Serilog.Extensions.Logging 并使用此 outputTemplate 输出到控制台: "{Timestamp:HH:mm} [{Level:u3}] {Messag
Date时间的RollingFile Sink的当前输出如下 2015-04-06 18:40:54.400 +10:00 [Information] Hello World! 无论如何,有没有要消除
我使用 Serilog 作为 .NET 的库,它为文件、控制台和其他地方提供诊断日志记录。 我的问题是我在我的代码中声明了一些日志记录事件,但是在查看 Windows 事件查看器时,分配给我的日志的事
Serilog 的 Azure 表存储接收器是否可以像其他接收器(例如 Elasticsearch 和 Seq)一样通过 app/Web.config 配置,这些接收器可以从配置文件进行配置。 Azu
我正在使用 SeriLog 登录基于 IdentityServer3 的身份验证服务。我刚刚将 serilog.sinks.literate 插件替换为 serilog.sinks.file 插件,以
我使用 Serilog 作为 .NET 的库,它为文件、控制台和其他地方提供诊断日志记录。 我的问题是我在我的代码中声明了一些日志记录事件,但是在查看 Windows 事件查看器时,分配给我的日志的事
我正在尝试安装 serilog,但出现错误 PM> Install-Package Serilog Install-Package : 'Serilog' already has a dependen
从 nlog 转移到 serilog,我希望我的 .NET 框架桌面应用程序在每次运行时重用一个静态命名的日志文件,但在每个新进程中清除文件的内容。可以这样配置serilog吗? This is a
serilog 的可用接收器位于此处:https://github.com/serilog/serilog/wiki/Provided-Sinks 但不包括蔚蓝服务巴士。 目前是否正在努力创建一个?如
我要写 Serilog Warning .NET 集合中的事件,以便它们可以包含在给用户的报告中。我该怎么做 configure the static Log.Logger 写信给类似 static
我尝试在结构化模式下将 JSNLog 与 Serilog 一起使用,如文档中所述: http://jsnlog.com/Documentation/HowTo/StructuredLogging 我在
我正在从 log4net 切换到 Serilog,但错过了我在 log4net 中的一些格式化可能性。我没有找到关于我可以在 outputTemplate 中使用哪些格式化程序的任何文档。有什么方法可
我远不是具有任何 .net 经验的开发人员,但工作中的开发团队希望使用 Serilog 和 serilog-sinks-elasticsearch 将日志推送到我的 ELK 堆栈中。 查看 seril
我是一名优秀的程序员,十分优秀!