1+ using Microsoft . AspNetCore . HttpLogging ;
12using Microsoft . AspNetCore . Mvc ;
23using Serilog . Context ;
4+ using System . Net ;
35
46namespace SerilogTest . Controllers ;
57
@@ -10,13 +12,21 @@ public class LogTestController : ControllerBase
1012
1113 #region Constants & Statics
1214
13- private static readonly string [ ] Summaries = [ "Freezing" , "Bracing" , "Chilly" , "Cool" , "Mild" , "Warm" , "Balmy" , "Hot" , "Sweltering" , "Scorching" ] ;
15+ private static void InnerMehtod ( int ? abc )
16+ {
17+ Console . WriteLine ( abc ) ;
18+
19+ throw new MyException ( "the InnerMehtod throw" ) ;
20+ }
1421
1522 #endregion
1623
17- private readonly ILogger < LogTestController > _logger ;
24+ private readonly string [ ] _summaries = [ "Freezing" , "Bracing" , "Chilly" , "Cool" , "Mild" , "Warm" , "Balmy" , "Hot" , "Sweltering" , "Scorching" ] ;
25+
1826 private readonly IHttpContextAccessor _httpContextAccessor ;
1927
28+ private readonly ILogger < LogTestController > _logger ;
29+
2030 public LogTestController ( ILogger < LogTestController > logger , IHttpContextAccessor httpContextAccessor )
2131 {
2232 _logger = logger ;
@@ -25,6 +35,42 @@ public LogTestController(ILogger<LogTestController> logger, IHttpContextAccessor
2535
2636 #region Methods
2737
38+ [ HttpGet ( "LogError" ) ]
39+ public IActionResult LogError ( )
40+ {
41+ _logger . LogError ( new MyException ( "LogError action called" ) , "this is error message." ) ;
42+
43+ return Ok ( ) ;
44+ }
45+
46+ [ HttpGet ( "LogErrorThrow" ) ]
47+ public IActionResult LogErrorThrow ( int ? abc )
48+ {
49+ InnerMehtod ( abc ) ;
50+
51+ return Ok ( ) ;
52+ }
53+
54+ [ HttpGet ( "LoggerScope" ) ]
55+ [ HttpLogging ( HttpLoggingFields . RequestPropertiesAndHeaders ) ]
56+ [ ProducesResponseType < WeatherForecast > ( ( ( int ) HttpStatusCode . OK ) ) ]
57+ public IActionResult LoggerScope ( )
58+ {
59+ using ( _logger . BeginScope ( "scope 1" ) )
60+ {
61+ // ["scope 1"]
62+ _logger . LogInformation ( "sth. 1" ) ;
63+
64+ using ( _logger . BeginScope ( "scope 2" ) )
65+ {
66+ // ["scope 1", "scope 2"]
67+ _logger . LogInformation ( "sth. 2" ) ;
68+ }
69+ }
70+
71+ return Ok ( ) ;
72+ }
73+
2874 [ HttpGet ( "LogWarning" ) ]
2975 public IEnumerable < WeatherForecast > LogWarning ( )
3076 {
@@ -41,7 +87,7 @@ public IEnumerable<WeatherForecast> LogWarning()
4187 DateTime = dateTime ,
4288 DateTimeOffset = DateTimeOffset . UtcNow ,
4389 TemperatureC = Random . Shared . Next ( - 20 , 55 ) ,
44- Summary = Summaries [ Random . Shared . Next ( Summaries . Length ) ]
90+ Summary = _summaries [ Random . Shared . Next ( _summaries . Length ) ]
4591 } ;
4692 } )
4793 . ToArray ( ) ;
@@ -51,38 +97,44 @@ public IEnumerable<WeatherForecast> LogWarning()
5197 return arr ;
5298 }
5399
54- [ HttpGet ( "LogError" ) ]
55- public IActionResult LogError ( )
56- {
57- _logger . LogError ( new MyException ( "LogError action called" ) , "this is error message." ) ;
58-
59- return Ok ( ) ;
60- }
61-
62- [ HttpGet ( "LogErrorThrow" ) ]
63- public IActionResult LogErrorThrow ( int ? abc )
100+ [ HttpGet ( "OnlyLogRequest" ) ]
101+ [ HttpLogging ( HttpLoggingFields . RequestPropertiesAndHeaders ) ]
102+ [ ProducesResponseType < WeatherForecast > ( ( ( int ) HttpStatusCode . OK ) ) ]
103+ public IActionResult OnlyLogRequest ( )
64104 {
65- InnerMehtod ( abc ) ;
66-
67- return Ok ( ) ;
68- }
69-
70- private static void InnerMehtod ( int ? abc )
71- {
72- Console . WriteLine ( abc ) ;
73-
74- throw new MyException ( "the InnerMehtod throw" ) ;
105+ var dateTime = DateTime . Now . AddDays ( 1 ) ;
106+
107+ return Ok (
108+ new WeatherForecast
109+ {
110+ Date = DateOnly . FromDateTime ( dateTime ) ,
111+ DateTime = dateTime ,
112+ DateTimeOffset = DateTimeOffset . UtcNow ,
113+ TemperatureC = Random . Shared . Next ( - 20 , 55 ) ,
114+ Summary = _summaries [ Random . Shared . Next ( _summaries . Length ) ]
115+ } ) ;
75116 }
76117
77118 [ HttpGet ( "UseLogContext" ) ]
119+ [ ProducesResponseType < WeatherForecast > ( ( ( int ) HttpStatusCode . OK ) ) ]
78120 public IActionResult UseLogContext ( )
79121 {
80122 using var _ = LogContext . PushProperty ( "TenantId" , Guid . NewGuid ( ) ) ;
81123 using var __ = LogContext . PushProperty ( "UserId" , 1 ) ;
82124
83125 _logger . LogInformation ( "method starting: {Method}" , nameof ( UseLogContext ) ) ;
84126
85- return Ok ( ) ;
127+ var dateTime = DateTime . Now . AddDays ( 1 ) ;
128+
129+ return Ok (
130+ new WeatherForecast
131+ {
132+ Date = DateOnly . FromDateTime ( dateTime ) ,
133+ DateTime = dateTime ,
134+ DateTimeOffset = DateTimeOffset . UtcNow ,
135+ TemperatureC = Random . Shared . Next ( - 20 , 55 ) ,
136+ Summary = _summaries [ Random . Shared . Next ( _summaries . Length ) ]
137+ } ) ;
86138 }
87139
88140 #endregion
0 commit comments