@@ -7,18 +7,18 @@ namespace OpenShock.Desktop.RepositoryServer.ExceptionHandler;
77public sealed class OpenShockExceptionHandler : IExceptionHandler
88{
99 private readonly IProblemDetailsService _problemDetailsService ;
10- private readonly ILogger _logger ;
11-
12- public OpenShockExceptionHandler ( IProblemDetailsService problemDetailsService , ILoggerFactory loggerFactory )
10+ private readonly ILogger < OpenShockExceptionHandler > _logger ;
11+
12+ public OpenShockExceptionHandler ( IProblemDetailsService problemDetailsService , ILogger < OpenShockExceptionHandler > logger )
1313 {
1414 _problemDetailsService = problemDetailsService ;
15- _logger = loggerFactory . CreateLogger ( "RequestInfo" ) ;
15+ _logger = logger ;
1616 }
1717
1818 public async ValueTask < bool > TryHandleAsync ( HttpContext context , Exception exception , CancellationToken cancellationToken )
1919 {
20+ _logger . LogError ( exception , "An unhandled exception occurred while processing the request." ) ;
2021 context . Response . StatusCode = ( int ) HttpStatusCode . InternalServerError ;
21- await PrintRequestInfo ( context ) ;
2222
2323 var responseObject = ExceptionError . Exception ;
2424 responseObject . AddContext ( context ) ;
@@ -30,30 +30,4 @@ public async ValueTask<bool> TryHandleAsync(HttpContext context, Exception excep
3030 ProblemDetails = responseObject
3131 } ) ;
3232 }
33-
34- private async Task PrintRequestInfo ( HttpContext context )
35- {
36- // Rewind our body reader, so we can read it again.
37- context . Request . Body . Seek ( 0 , SeekOrigin . Begin ) ;
38- // Used to read from the body stream.
39- using var stream = new StreamReader ( context . Request . Body ) ;
40-
41- // Create Dictionaries to be logging in our RequestInfo object for both Header values and Query parameters.
42- var headers = context . Request . Headers . ToDictionary ( x => x . Key , x => x . Value . ToString ( ) ) ;
43- var queryParams = context . Request . Query . ToDictionary ( x => x . Key , x => x . Value . ToString ( ) ) ;
44-
45- // Create our RequestInfo object.
46- var requestInfo = new RequestInfo
47- {
48- Body = await stream . ReadToEndAsync ( ) ,
49- Headers = headers ,
50- TraceId = context . TraceIdentifier ,
51- Method = context . Request . Method ,
52- Path = context . Request . Path . Value ,
53- Query = queryParams
54- } ;
55-
56- // Finally log this object on Information level.
57- _logger . LogInformation ( "{@RequestInfo}" , requestInfo ) ;
58- }
5933}
0 commit comments