服务启动后我看不到 Serilog 消息
我尝试在 Web 应用程序
上实践学习 Serilog。启动后我看不到消息。当我调试时,我成功进入 OnGet()
方法并触发异常。但是,信息和错误消息均未显示。我只得到 -->应用程序正在启动 ....
消息,该消息在 Program.cs
中定义。
我希望看到计数和错误消息作为输出。我错过了什么?它不会读取 appsettings.json
吗?如果它没有被读取,它不应该覆盖微软的消息,但它会覆盖。
Program.cs
using Serilog;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
WebApplication app;
try
{
Log.Logger = new LoggerConfiguration().CreateLogger();
builder.Host.UseSerilog((
(ctx, lc) => lc
.ReadFrom.Configuration(ctx.Configuration))
);
app = builder.Build();
app.UseSerilogRequestLogging();
Log.Information("--> App is starting up ....");
}
catch (Exception e)
{
Log.Fatal(e, "The app failed to start bruh!!");
throw;
}
finally
{
Log.CloseAndFlush();
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
Index.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace SeriLogApp.Pages;
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
_logger.LogInformation("My exercise app web app logging is here <----");
try
{
for (int i = 0; i < 100; i++)
{
if (i == 56)
{
throw new Exception("it our on purpose exception");
}
else
{
_logger.LogInformation("the value {LoopCountValue}", i);
}
}
}
catch (Exception e)
{
_logger.LogError(e, "--> I caught you man yoooo!! <<***--");
}
}
}
appsettings.json
{
"AllowedHosts": "*",
"Serilog": {
"Using": [],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
{ "Name": "Console" }
]
}
}
应用程序的 csproj 内容,其中包括必要的 nuget 包。
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
<PackageReference Include="Serilog.Enrichers.Context" Version="4.2.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
</ItemGroup>
</Project>
I try to learn Serilog in practice on a Web App
. I can't see the messages after it is run up. When I debug, I'm in successfully OnGet()
method and trigger the exception. However, both information and error messages aren't shown. I get only --> App is starting up ....
message, which is defined in the Program.cs
I expect to see count numbers and the error message as output. What point do I miss? Doesn't it read the appsettings.json
? If it is not read, it shouldn't override Microsoft's messages, but it overrides.
Program.cs
using Serilog;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
WebApplication app;
try
{
Log.Logger = new LoggerConfiguration().CreateLogger();
builder.Host.UseSerilog((
(ctx, lc) => lc
.ReadFrom.Configuration(ctx.Configuration))
);
app = builder.Build();
app.UseSerilogRequestLogging();
Log.Information("--> App is starting up ....");
}
catch (Exception e)
{
Log.Fatal(e, "The app failed to start bruh!!");
throw;
}
finally
{
Log.CloseAndFlush();
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
Index.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace SeriLogApp.Pages;
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
_logger.LogInformation("My exercise app web app logging is here <----");
try
{
for (int i = 0; i < 100; i++)
{
if (i == 56)
{
throw new Exception("it our on purpose exception");
}
else
{
_logger.LogInformation("the value {LoopCountValue}", i);
}
}
}
catch (Exception e)
{
_logger.LogError(e, "--> I caught you man yoooo!! <<***--");
}
}
}
appsettings.json
{
"AllowedHosts": "*",
"Serilog": {
"Using": [],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"WriteTo": [
{ "Name": "Console" }
]
}
}
The app's csproj content which includes necessary nuget packages.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
<PackageReference Include="Serilog.Enrichers.Context" Version="4.2.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.2.0" />
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.2" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
</ItemGroup>
</Project>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需删除
线
Just remove
line