Skip to content

Commit d974b8d

Browse files
committed
AddHttpLogging
1 parent 1b860c3 commit d974b8d

4 files changed

Lines changed: 52 additions & 14 deletions

File tree

src/plugins/SerilogTest/Controllers/LogTestController.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Mvc;
2+
using Serilog.Context;
23

34
namespace SerilogTest.Controllers;
45

@@ -14,10 +15,12 @@ public class LogTestController : ControllerBase
1415
#endregion
1516

1617
private readonly ILogger<LogTestController> _logger;
18+
private readonly IHttpContextAccessor _httpContextAccessor;
1719

18-
public LogTestController(ILogger<LogTestController> logger)
20+
public LogTestController(ILogger<LogTestController> logger, IHttpContextAccessor httpContextAccessor)
1921
{
2022
_logger = logger;
23+
_httpContextAccessor = httpContextAccessor;
2124
}
2225

2326
#region Methods
@@ -71,6 +74,17 @@ private static void InnerMehtod(int? abc)
7174
throw new MyException("the InnerMehtod throw");
7275
}
7376

77+
[HttpGet("UseLogContext")]
78+
public IActionResult UseLogContext()
79+
{
80+
using var _ = LogContext.PushProperty("TenantId", Guid.NewGuid());
81+
using var __ = LogContext.PushProperty("UserId", 1);
82+
83+
_logger.LogInformation("method starting: {Method}", nameof(UseLogContext));
84+
85+
return Ok();
86+
}
87+
7488
#endregion
7589

7690
}

src/plugins/SerilogTest/Program.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.AspNetCore.HttpLogging;
12
using Serilog;
23
using Serilog.Core;
34
using Serilog.Exceptions;
@@ -46,12 +47,28 @@ private static async Task Main(string[] args)
4647
_ = ConfigureLogger(options);
4748
});
4849

49-
_ = builder.Services.AddControllers();
50+
var services = builder.Services;
51+
_ = services.AddControllers();
52+
_ = services.AddHttpContextAccessor();
5053
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
51-
_ = builder.Services.AddOpenApi();
54+
_ = services.AddOpenApi();
55+
56+
_ = services
57+
.AddHttpLogging(
58+
(options) =>
59+
{
60+
options.LoggingFields = HttpLoggingFields.All;
61+
});
62+
63+
_ = services.AddHealthChecks();
5264

5365
var app = builder.Build();
5466

67+
_ = app.UseForwardedHeaders();
68+
_ = app.UseHttpsRedirection();
69+
70+
_ = app.UseStaticFiles();
71+
5572
// Configure the HTTP request pipeline.
5673
if (app.Environment.IsDevelopment())
5774
{
@@ -63,6 +80,10 @@ private static async Task Main(string[] args)
6380
_ = app.UseHsts();
6481
}
6582

83+
_ = app.UseHttpLogging();
84+
85+
_ = app.UseHealthChecks("/health");
86+
6687
//_ = app.UseSerilogRequestLogging(
6788
// (options) =>
6889
// {
@@ -74,8 +95,6 @@ private static async Task Main(string[] args)
7495
// };
7596
// });
7697

77-
_ = app.UseHttpsRedirection();
78-
7998
_ = app.UseAuthorization();
8099

81100
_ = app.MapControllers();

src/plugins/SerilogTest/SerilogTest.http

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ GET {{SerilogTest_HostAddress}}/LogTest/LogErrorThrow/
1414
Accept: application/json
1515

1616
###
17+
18+
GET {{SerilogTest_HostAddress}}/LogTest/UseLogContext/
19+
Accept: application/json
20+
21+
###

src/plugins/SerilogTest/appsettings.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
"WithProcessId",
1616
"WithProcessName",
1717
"WithThreadId",
18-
"WithThreadName",
19-
"WithClientIp",
20-
"WithCorrelationId",
21-
{
22-
"Name": "WithRequestHeader",
23-
"Args": {
24-
"headerName": "User-Agent"
25-
}
26-
}
18+
"WithThreadName"
19+
// "WithClientIp",
20+
// "WithCorrelationId",
21+
// {
22+
// "Name": "WithRequestHeader",
23+
// "Args": {
24+
// "headerName": "User-Agent"
25+
// }
26+
// }
2727
],
2828
"WriteTo": [
2929
{

0 commit comments

Comments
 (0)