RollingFile接收器的当前输出日期时间如下
2015-04-06 18:40:54.400 +10:00 [Information] Hello World!
Run Code Online (Sandbox Code Playgroud)
无论如何要删除TimeZone偏移量?+10:00.实现以下输出;
2015-04-06 18:40:54.400 [Information] Hello World!
Run Code Online (Sandbox Code Playgroud)
在我的情况下,时区偏移是多余的绒毛,这只是混乱我的文本日志.
在我的 appsettings.json 我写过:
{
"Logging": {
"PathLogsFile": "./Logs",
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
Run Code Online (Sandbox Code Playgroud)
并在我的startup.cs文件中的构造函数中
var pathLogsFile = Configuration["Logging:PathLogsFile"];
var logLevelApp = Configuration.GetSection("Logging:LogLevel:Default");
Log.Logger = new LoggerConfiguration()
.ReadFrom.KeyValuePairs(new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>(logLevelApp.Key, logLevelApp.Value)
}.ToDictionary(x => x.Key, x => x.Value))
.Enrich.FromLogContext()
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(evt => evt.Level == Serilog.Events.LogEventLevel.Error)
.WriteTo.RollingFile(Path.Combine(pathLogsFile, "error-{Date}.log")))
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(evt => evt.Level >= Serilog.Events.LogEventLevel.Debug)
.WriteTo.RollingFile(Path.Combine(pathLogsFile, "log-{Date}.log")))
.CreateLogger();
Run Code Online (Sandbox Code Playgroud)
但是从 appsettings 读取的最低级别不起作用。有任何想法吗?我该如何解决?
Serilog 不会创建日志文件 - 因此(我认为)不会记录任何内容。由于目标应该是微服务,我使用的是 CQRS 模式。
这是我的 Startup.cs
private readonly ISession session;
private readonly ILogger logger;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
var baseDirectory = Directory.GetDirectoryRoot(AppDomain.CurrentDomain.BaseDirectory);
Log.Logger = new LoggerConfiguration()
.WriteTo
.Logger(lc => lc.Filter
.ByIncludingOnly(e => e.Level == LogEventLevel.Information || e.Level == LogEventLevel.Debug)
.WriteTo.RollingFile(Path.Combine(baseDirectory, "/Logs/info.log")))
.WriteTo
.Logger(lc => lc.Filter
.ByIncludingOnly(e => e.Level == LogEventLevel.Error)
.WriteTo.RollingFile(Path.Combine(baseDirectory, "/Logs/error.log")))
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
loggerFactory.AddSerilog();
...//To shorten this example in Stackoverflow, I've …Run Code Online (Sandbox Code Playgroud) 我有一个自定义的 Enricher:CorrelationIdsEnricher为了编写CorrelationId和RequestId记录,它的构造函数有一个参数:ICorrelationContextProvider用于传递相关上下文提供程序。
在我的项目中,我通过阅读appsettings.json配置文件来配置 serilog 。这是配置文件:
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Common.Logging", "Common.Correlation" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] [{RequestId} {CorrelationId}] {Message}{NewLine}{Exception}",
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console"
}
}
],
"Enrich": [
"FromLogContext",
{
"Name": "WithCorrelationIds",
"Args": {
"provider": "Correlation.ServerContextProvider::Default, Common.Correlation"
}
}
],
}
}
Run Code Online (Sandbox Code Playgroud)
但是它不能CorrelationIdsEnricher正确设置。
有谁知道为什么?
我在Asp点网核心应用程序中使用Serilog。我希望我的日志文件易于阅读,但也可以轻松解析。
我遇到的问题是使用换行符记录了异常。某些Microsoft事件具有包含换行符的消息。
我希望能够使用每行一个事件来解析日志文件。
我可以编写自己的ITextFormatter实现,用\ r \ n替换新行,但这意味着我需要复制MessageTemplateTextFormatter和其他类中的许多逻辑。
我正在使用 Serilog 将日志写入我的 .NET Core 应用程序中的AWS Elasticsearch Service,但在登录 Kibana 时,我没有看到任何写入的日志。
public Startup(IConfiguration configuration, IHostingEnvironment hostingEnvironment)
{
const string esUrl = "https://aws-es-thinger.us-west-1.es.amazonaws.com";
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails()
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(esUrl))
{
ModifyConnectionSettings = conn =>
{
var httpConnection = new AwsHttpConnection("us-east-1");
var pool = new SingleNodeConnectionPool(new Uri(esUrl));
var conf = new ConnectionConfiguration(pool, httpConnection);
return conf;
},
AutoRegisterTemplate = true
}).CreateLogger();
}
Run Code Online (Sandbox Code Playgroud)
我可以用来HttpClient成功获得响应。
此外,我还可以从浏览器加载 Kibana 和 ElasticSearch 网址。请帮我解决我在这里缺少的东西。
编辑 在启动时连接时出现以下错误:
System.Net.Http.WinHttpException:无法建立与服务器的连接
asp.net-mvc amazon-web-services elasticsearch serilog amazon-elasticsearch
所以我正在将日志框架从Log4Net交换到Serilog,我们在那里注销文本文件.
我希望文本文件的格式与之前完全相同,因此用户可以无缝迁移.
所以使用Serilog文件接收器,我能够修改它outputTemplate以满足我的需要,但是我无法让Serilogs日志级别与Log4Net中的完全相同.
我希望:
通过阅读outputTemplates的文档 https://github.com/serilog/serilog/wiki/Configuration-Basics#output-templates
对于更紧凑的级别名称,请分别对三个字符的大写或小写级别名称使用{Level:u3}或{Level:w3}等格式.
所以我尝试了以下尝试获得5个大写的字母{Level:u5}然后回来:
请问有人指点这个吗?
我正在将 Serilog 与 Seq 一起使用,并希望使用我自己的属性来丰富 Seq 中出现的日志记录。
如果我输入一个日志语句,如...
Log.Information("ProcessCycle {Site} {Activity}", SiteName, ActivityName);
Run Code Online (Sandbox Code Playgroud)
在 Seq 我得到...
请注意,站点和活动值在 Seq 中显示为丰富的属性,但它们也显示在整体消息中。
如何记录获取丰富属性的位置,但文本消息行中没有显示值?请注意,我有 NuGet 包,它为每个调用添加了一个 ThreadId。我希望 Site 和 Activity 属性位于丰富的道具列表中,但不一定打印在消息行中。
对此的答案可能还需要了解我们的应用程序。
该应用程序是一个 Windows 服务,它产生多个做不同事情的活动。因此,Windows 服务编排了其中包含的各种活动。按照计划,它只是在每个活动上调用“流程”来完成一些工作。每次流程被编排器调用时,我都需要该 Activity 的所有日志记录以自动包含如上所示的 Site 和 Activity 值(以及更多属性值,但我不希望它们全部打印在消息行中)。
因此,我们将看到的不是上面的条目,而是... 注意消息现在只显示为“ProcessCycle”。
我正在将在网络核心中完成的应用程序从 Azure 迁移到 AWS。后端是一个 Asp.NetCore WebApi,它使用 Serilog 进行日志记录。我只是将它放在 Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction 的子类后面,现在它在 lambda 函数中运行。令我困惑的是,使用 Serilog 生成的日志如何自动转到这个已部署的 lambda 函数的 Cloudwatch。有人能解释一下这些日志库是如何工作的吗,也许 Lambda nuget 包中的日志实体可以自动点击?
我们正在将日志记录从 log4net 转移到 Serilog。我们从 log4net 登录到我们的远程 MSSQL 服务器。我们需要保持该表和列完整,因为其他团队正在使用它,但我们需要将我们的 Serilog 日志发送到其中。查看https://github.com/serilog/serilog-sinks-mssqlserver我看不到我们如何将旧列链接到日志数据。我可以在 Columns 标记中添加列并删除所有标准列,但我不知道如何将 serilog 数据记录到这些列中。
所以这是我们当前的日志表:
CREATE TABLE [dbo].[EventLog]
(
[EventID] [INT] IDENTITY(1,1) NOT NULL,
[Date] [DATETIME] NOT NULL,
[Type] [VARCHAR](10) NOT NULL,
[Logger] [VARCHAR](255) NOT NULL,
[Thread] [VARCHAR](255) NOT NULL,
[Message] [VARCHAR](MAX) NOT NULL,
[MoResponse] [VARCHAR](MAX) NULL,
[StackTrace] [VARCHAR](MAX) NULL,
[MoStatus] [VARCHAR](255) NULL,
[MoTransactionID] [VARCHAR](255) NULL,
CONSTRAINT [PK_EventLog]
PRIMARY KEY CLUSTERED ([EventID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = …Run Code Online (Sandbox Code Playgroud) serilog ×10
c# ×4
asp.net-core ×3
.net ×2
logging ×2
appsettings ×1
asp.net ×1
asp.net-mvc ×1
aws-lambda ×1
seq-logging ×1
xml ×1