Serilog + Azure - Log Analytics 工作区中没有出现自定义日志
Serilog + Azure - No custom logs appearing in Log Analytics workspace
我正在尝试使用 Serilog 从应用程序服务登录到 Log Analytics 工作区,但我的任何控制器(使用 DI 使用 ILogger)都没有记录到那里。
有什么明显的我遗漏的东西吗?工作区没有显示自定义日志,并且对 API return 的调用是他们期望的 return.
public class Program
{
public static void Main(string[] args)
{
// The initial "bootstrap" logger is able to log errors during start-up. It's completely replaced by the
// logger configured in `UseSerilog()` below, once configuration and dependency-injection have both been
// set up successfully.
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
Log.Information("Starting up!");
try
{
CreateHostBuilder(args).Build().Run();
Log.Information("Stopped cleanly");
return;
}
catch (Exception ex)
{
Log.Fatal(ex, "An unhandled exception occured during bootstrapping");
return;
}
finally
{
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Controllers
services.AddControllers().AddNewtonsoftJson();
// Web
services.AddWebRegistry(Configuration);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
// Write streamlined request completion events, instead of the more verbose ones from the framework.
// To use the default framework request logging instead, remove this line and set the "Microsoft"
// level in appsettings.json to "Information".
app.UseSerilogRequestLogging();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
{
"Serilog": {
"Using": [ "Serilog.Sinks.AzureAnalytics" ],
"MinimumLevel": "Debug",
"Override": {
"System": "Information",
"Microsoft": "Information",
"Microsoft.AspNetCore.Authentication": "Information",
"Microsoft.AspNetCore.SignalR": "Debug",
"Microsoft.AspNetCore.Http.Connections": "Debug"
},
"WriteTo": [
{
"Name": "AzureAnalytics",
"Args": {
"logName": "devlog",
"authenticationId": "secret",
"workspaceId": "secret"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithThreadName", "WithEventType" ]
},
我认为您缺少将日志从 Serilog 接收到 Log Analytics 的包:
添加以下包:
Serilog.AspNetCore
Serilog.Sinks.AzureAnalytics
然后:
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.AzureAnalytics(workspaceId: "WORKSPACE ID",
authenticationId: "PRIMARY KEY")
.CreateLogger();
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog();
我正在尝试使用 Serilog 从应用程序服务登录到 Log Analytics 工作区,但我的任何控制器(使用 DI 使用 ILogger)都没有记录到那里。
有什么明显的我遗漏的东西吗?工作区没有显示自定义日志,并且对 API return 的调用是他们期望的 return.
public class Program
{
public static void Main(string[] args)
{
// The initial "bootstrap" logger is able to log errors during start-up. It's completely replaced by the
// logger configured in `UseSerilog()` below, once configuration and dependency-injection have both been
// set up successfully.
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
Log.Information("Starting up!");
try
{
CreateHostBuilder(args).Build().Run();
Log.Information("Stopped cleanly");
return;
}
catch (Exception ex)
{
Log.Fatal(ex, "An unhandled exception occured during bootstrapping");
return;
}
finally
{
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Controllers
services.AddControllers().AddNewtonsoftJson();
// Web
services.AddWebRegistry(Configuration);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
// Write streamlined request completion events, instead of the more verbose ones from the framework.
// To use the default framework request logging instead, remove this line and set the "Microsoft"
// level in appsettings.json to "Information".
app.UseSerilogRequestLogging();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
{
"Serilog": {
"Using": [ "Serilog.Sinks.AzureAnalytics" ],
"MinimumLevel": "Debug",
"Override": {
"System": "Information",
"Microsoft": "Information",
"Microsoft.AspNetCore.Authentication": "Information",
"Microsoft.AspNetCore.SignalR": "Debug",
"Microsoft.AspNetCore.Http.Connections": "Debug"
},
"WriteTo": [
{
"Name": "AzureAnalytics",
"Args": {
"logName": "devlog",
"authenticationId": "secret",
"workspaceId": "secret"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithThreadName", "WithEventType" ]
},
我认为您缺少将日志从 Serilog 接收到 Log Analytics 的包:
添加以下包:
Serilog.AspNetCore
Serilog.Sinks.AzureAnalytics
然后:
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.AzureAnalytics(workspaceId: "WORKSPACE ID",
authenticationId: "PRIMARY KEY")
.CreateLogger();
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog();