我想将 SerilogWarning事件写入.NET 集合,以便将它们包含在给用户的报告中。如何将静态配置Log.Logger为写入类似 a 的内容static List<string<>?
我在提供的接收器列表中看到了Rx Observer,这是唯一一个似乎很容易使 .NET 对象可用的人,或者是否有我错过的明显替代方案?
或者,这是一种愚蠢的方法吗 - 有没有更好的方法来收集Warning事件以将其添加到面向用户的报告中?
我已启用 SeriLog(最新版本)自记录功能,并且看到数百条消息说
达到最大解构深度
不知道这意味着什么以及这是否是我需要担心的问题。
有谁知道是什么触发了这条消息以及我是否做错了什么?
我想在 IIS 上启用文件日志记录以解决本地 IIS 上的问题。以下是我正在使用的代码。当我在 Visual Studio 上运行并记录到文件时它有效,但当我部署到 IIS 时无效。文件夹有足够的权限来创建文件,我也创建了文件夹和文件。我在这里错过了什么?
程序.cs
var path = @"C:\workspace\Logs\Log-{Date}.txt";
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console(
outputTemplate:
"[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}",
theme: AnsiConsoleTheme.Literate)
.WriteTo.File(path, fileSizeLimitBytes: 1_000_000,
rollOnFileSizeLimit: true,
shared: true,
flushToDiskInterval: TimeSpan.FromSeconds(1))
.CreateLogger();
Run Code Online (Sandbox Code Playgroud)
启用 useSerialLog()
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureServices(services => services.AddAutofac())
.UseStartup<Startup>()
.UseSerilog()
.Build();
Run Code Online (Sandbox Code Playgroud) 在 Kubernetes (Minikube) 上使用 EFK 堆栈。有一个使用 Serilog 的 asp.net 核心应用程序以 Json 的形式写入控制台。日志确实会发送到 Elasticsearch,但它们到达未解析的字符串,进入“日志”字段,这就是问题所在。
这是控制台输出:
{
"@timestamp": "2019-03-22T22:08:24.6499272+01:00",
"level": "Fatal",
"messageTemplate": "Text: {Message}",
"message": "Text: \"aaaa\"",
"exception": {
"Depth": 0,
"ClassName": "",
"Message": "Boom!",
"Source": null,
"StackTraceString": null,
"RemoteStackTraceString": "",
"RemoteStackIndex": -1,
"HResult": -2146232832,
"HelpURL": null
},
"fields": {
"Message": "aaaa",
"SourceContext": "frontend.values.web.Controllers.HomeController",
"ActionId": "0a0967e8-be30-4658-8663-2a1fd7d9eb53",
"ActionName": "frontend.values.web.Controllers.HomeController.WriteTrace (frontend.values.web)",
"RequestId": "0HLLF1A02IS16:00000005",
"RequestPath": "/Home/WriteTrace",
"CorrelationId": null,
"ConnectionId": "0HLLF1A02IS16",
"ExceptionDetail": {
"HResult": -2146232832,
"Message": "Boom!",
"Source": null,
"Type": "System.ApplicationException"
}
}
} …Run Code Online (Sandbox Code Playgroud) 我试图将 serilog 配置为写入多个文件,但没有任何运气。使用此配置它只是写入第二个文件?
{
"AllowedHosts": "*",
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "c:\\temp\\audit-.log",
"rollingInterval": "Day",
"restrictedToMinimumLevel": "Information"
}
},
{
"Name": "File",
"Args": {
"path": "c:\\temp\\error-.log",
"rollingInterval": "Day",
"restrictedToMinimumLevel": "Error"
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
或者有什么方法可以将许多记录器加载到具有来自 appsettings.json 的不同配置的软件中。像这样的东西?
var errorLogConfiguration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddEnvironmentVariables()
.Build();
_log = new LoggerConfiguration()
.ReadFrom
.Configuration(errorLogConfiguration)
.CreateLogger();
Run Code Online (Sandbox Code Playgroud) 我在我的 ASP Core 2.2 应用程序中使用了 serilog。一切正常,但我无法设置flushToDiskInterval。例如,这意味着我想每分钟将日志刷新到磁盘,但日志会在创建时刷新。我的 Program.cs 文件:
public class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.File("Logs/log-{Date}.txt", buffered: true, flushToDiskInterval: TimeSpan.FromSeconds(60))
.CreateLogger();
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseSerilog() // Set serilog as the logging provider.
.UseStartup<Startup>();
}
Run Code Online (Sandbox Code Playgroud)
那么问题来了,如何设置间隔呢?
==更新==
我无法理解,但现在一切正常……我检查过,确实设置了冲洗间隔。
我想直接使用Microsoft.LoggerSerilog 的BeginScope功能,该功能。
我知道我可以使用 MicrosoftILogger并将 Serilog 设置为底层记录器,但在应用程序的这一点上太麻烦了。
我想知道BeginScopeSerilog 中是否有等价物。
互联网上的信息并不直接,有人提到 Serilog 支持,但如果直接支持或使用 Microsoft 类,则没有提及。
我正在开发 ASP.NET Core 3.1 应用程序。我想将事件记录到文件中并能够在应用程序运行时读取它们。为此,我尝试使用Serilog.Extensions.Logging.File NuGet 包,然后指定日志文件路径如下:
启动文件
public void Configure(IApplicationBuilder app, ILoggerFactory logFactory)
{
logFactory.AddFile("Log");
}
Run Code Online (Sandbox Code Playgroud)
任何以这种方式读取或写入文件的尝试
string ReadLog(string logPath)
{
return System.IO.File.ReadAllText(logPath);
}
Run Code Online (Sandbox Code Playgroud)
以System.IO.IOException: 'The process cannot access the file {log-path} because it is being used by another process.'异常结束。
编辑:我已经安装了Serilog.AspNetCore并进行了如下所示的更改,同时还从配置功能中删除了 logFactory。但异常继续发生。
程序.cs
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.File(
"Logs/log-.txt",
shared: true,
flushToDiskInterval: TimeSpan.FromSeconds(5),
rollingInterval: RollingInterval.Day)
.CreateLogger();
try
{
Log.Information("Starting web host"); …Run Code Online (Sandbox Code Playgroud) 我想知道如何在 Azure 函数中使用 Serilog。我在我的函数中创建了一个启动类,设置如下:
var loggerConfiguration = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
builder.Services.AddLogging(op => op.AddSerilog(loggerConfiguration));
Run Code Online (Sandbox Code Playgroud)
如何将 serilog 中的 ILogger 注入到我的函数中?
using Serilog;
namespace LogFunction
public class XX
{
[FunctionName("XX")]
public void Run([TimerTrigger("*/60 * * * * *")]TimerInfo myTimer, ILogger log)
{
log.Information("Log this object {Car}", car); //using serilog not Microsoft.Extensions.Logging
}
}
Run Code Online (Sandbox Code Playgroud)
正如您在上面看到的,我引用的是来自 Serilog 而不是 Microsoft Logging 的 ILogger。
我正在开发 .NET 6 API,并尝试使用 Serilog 记录所有用户操作。
我想做到这一点,而不必添加_logger.Info(...);用户请求和响应的每个端点。
在互联网上查看后,我发现可以使用UseSerilogRequestLogging(). 它工作得很好,但问题在于敏感数据。出于安全原因,某些数据(例如电子邮件、密码等)不应位于日志中。
所以问题是:如何记录所有用户请求和响应,同时出于安全原因隐藏敏感数据?
此 API 的一些代码:
获取日志中的数据:
app.UseHttpLogging()
.UseSerilogRequestLogging();
Run Code Online (Sandbox Code Playgroud)
builder.Services.AddHttpLogging(logging =>
{
logging.LoggingFields = HttpLoggingFields.All;
logging.RequestHeaders.Add(HeaderNames.Accept);
logging.RequestHeaders.Add(HeaderNames.ContentType);
logging.RequestHeaders.Add(HeaderNames.ContentDisposition);
logging.RequestHeaders.Add(HeaderNames.ContentEncoding);
logging.RequestHeaders.Add(HeaderNames.ContentLength);
logging.MediaTypeOptions.AddText("application/json");
logging.MediaTypeOptions.AddText("multipart/form-data");
logging.RequestBodyLogLimit = 4096;
logging.ResponseBodyLogLimit = 4096;
});
Run Code Online (Sandbox Code Playgroud)
记录配置并尝试隐藏敏感数据:
builder.Host.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.Enrich.With<UserEnricher>()
.Destructure.ByTransforming<AddUserDto>(_ => new AddUserDto()
{
UserName = _.UserName, UserDescription = _.UserDescription, Password = "****", Email = "****"
})
.Destructure.ByTransforming<UserLoginDto>(_ => new UserLoginDto()
{
UserName = _.UserName,
Password = "****"
}) …Run Code Online (Sandbox Code Playgroud) serilog ×10
c# ×4
asp.net-core ×3
logging ×3
.net-6.0 ×1
.net-core ×1
api ×1
fluent-bit ×1
iis ×1
kubernetes ×1
security ×1