【发布时间】:2022-09-26 17:01:23
【问题描述】:
我用Serilog在我的Blazor 服务器侧应用程序,部署在IIS使用站点绑定
我想确保将这些站点上的日志(未处理的异常和我的自定义日志信息)写入不同的文件夹由主机名.
我的Serilog 配置:
public static class HostBuilderExtension
{
public static IHostBuilder AddSerilog(this IHostBuilder hostBuilder)
{
return hostBuilder.UseSerilog((hostingContext, loggerConfiguration) =>
{
var appSettings = hostingContext.Configuration.Get<AppSettings>();
loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext()
.WriteTo.Map(\"Hostname\", \"ms-hosting\", (hostname, wr) =>
wr.Async(to =>
to.File(appSettings.GeneralLogsPath(hostname), rollingInterval: RollingInterval.Day, shared: true)));
});
}
}
一般日志路径
public string GeneralLogsPath(string hostname) => Path.Combine(AppLogsRoot, hostname, \"General\", \"log.log\");
登记在程序.cs:
builder.Host.AddSerilog();
还有我的习惯中间件将当前主机名推送到 LogContext:
using Serilog.Context;
using System.Collections.Generic;
namespace Herbst.Acc2.CustomerWebUI.Classes;
public class ScopedLoggingMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<ScopedLoggingMiddleware> _logger;
public ScopedLoggingMiddleware(RequestDelegate next, ILogger<ScopedLoggingMiddleware> logger)
{
_next = next ?? throw new ArgumentNullException(nameof(next));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task Invoke(HttpContext context)
{
if (context == null) throw new ArgumentNullException(nameof(context));
var hostname = context.Request.Host.Host;
try
{
using (LogContext.PushProperty(\"Hostname\", hostname))
{
await _next(context);
}
}
//To make sure that we don\'t loose the scope in case of an unexpected error
catch (Exception ex) when (LogOnUnexpectedError(ex))
{
return;
}
}
private bool LogOnUnexpectedError(Exception ex)
{
_logger.LogError(ex, \"An unexpected exception occured!\");
return true;
}
}
public static class ScopedLoggingMiddlewareExtensions
{
public static IApplicationBuilder UseScopedLogging(this IApplicationBuilder builder)
{
return builder.UseMiddleware<ScopedLoggingMiddleware>();
}
}
在 Program.cs 中
app.UseScopedLogging();
我可以确定来自test-t1.com 永远不会写至\\logs\\test-t2.com?
-
你测试过你的代码吗?你有遇到什么不寻常的情况吗?
-
@samwu,这段代码运行良好(至少在我的测试中)。特别是,我想了解为什么如果我从 test-t1.com 中间件推送主机名并在 test-t2.com 上突然出现未处理的异常(未处理中间件中的请求),它将被记录在 \\logs\\测试-t2.com。
-
您可以分享有关未处理异常的详细信息吗?是否可以使用 try catch 调试代码以查看异常原因?
-
@samwu,我自己抛出这个异常,看看如果我将 LogContext 属性推送到另一个 url 中它会写在哪里
-
我在您的帖子中没有看到异常,或者您可以尝试通过以下方式打开案例:support.microsoft.com。
标签: c# logging iis blazor serilog-aspnetcore