我想在客户端和服务器端的 blazor web assembly net 6 应用程序中使用 serilog。
在本文 https://nblumhardt.com/2019/11/serilog-blazor/我发现了如何将日志条目中继到服务器,以便将它们写入日志文件中。
然而,在这种方法中Log
静态类用于显式添加日志条目。
我想添加 serilog 作为日志记录提供程序,以便也记录异常和自动生成的信息。
在服务器端我使用
var builder = WebApplication.CreateBuilder(args);
builder.Host
.UseSerilog((ctx, lc) =>
{
lc.ReadFrom.Configuration(ctx.Configuration);
});
这样所有的东西都会传递给serilog。
UseSerilog
在 Serilog.AspNetCore 中定义。不幸的是,如果我将 Serilog.AspNetCore 添加到我的客户端项目中,则会出现以下错误:
NETSDK1082 没有 Microsoft.AspNetCore.App 的运行时包
可用于指定的 RuntimeIdentifier 'browser-wasm'
有没有办法手动将 serilog 添加到日志记录提供程序,或者有没有办法将 Serilog.AspNetCore 包添加到客户端项目?
我已经能够通过添加以下内容将 Serilog 添加到我的客户端应用程序日志记录提供程序Serilog.Extensions.LoggingNuGet 包。
然后我使用了以下代码:
using Serilog;
using Serilog.Core;
using Serilog.Extensions.Logging;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
/* ... */
/* Serilog configuration
* here I use BrowserHttp sink to send log entries to my Server app
*/
var levelSwitch = new LoggingLevelSwitch();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(levelSwitch)
.Enrich.WithProperty("InstanceId", Guid.NewGuid().ToString("n"))
.WriteTo.BrowserHttp(endpointUrl: $"{builder.HostEnvironment.BaseAddress}ingest", controlLevelSwitch: levelSwitch)
.CreateLogger();
/* this is used instead of .UseSerilog to add Serilog to providers */
builder.services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)