Skip to content

Commit b5d2cb6

Browse files
authored
Structured Logging (Service Project) (#1666)
## Why make this change? - Adds structured logging to the Core project per #1517 ## What is this change? - In instances of ilogger usage, removes interpolated strings and uses structured logging paradigm: - in the `message` parameter, names in curly brackets are names that can be filtered in a log view such as app insights/log analytics. The parameters after message are the variables which supply the values for the names in curly brackets. ```csharp _logger.LogDebug( message: "{correlationId} Request authentication state: {requestAuthStatus}.", HttpContextExtensions.GetLoggerCorrelationId(httpContext), requestAuthStatus); ``` ## testing Testing is manual via analyzing the console, no logic changes were made to the engine.
1 parent f8373b8 commit b5d2cb6

3 files changed

Lines changed: 35 additions & 19 deletions

File tree

src/Service/Controllers/ConfigurationController.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading.Tasks;
66
using Azure.DataApiBuilder.Config;
77
using Azure.DataApiBuilder.Core.Configurations;
8+
using Azure.DataApiBuilder.Core.Models;
89
using Microsoft.AspNetCore.Mvc;
910
using Microsoft.Extensions.Logging;
1011

@@ -53,12 +54,17 @@ public async Task<ActionResult> Index([FromBody] ConfigurationPostParametersV2 c
5354
}
5455
else
5556
{
56-
_logger.LogError($"Failed to initialize configuration.");
57+
_logger.LogError(
58+
message: "{correlationId} Failed to initialize configuration.",
59+
HttpContextExtensions.GetLoggerCorrelationId(HttpContext));
5760
}
5861
}
5962
catch (Exception e)
6063
{
61-
_logger.LogError($"Exception during configuration initialization. {e}");
64+
_logger.LogError(
65+
exception: e,
66+
message: "{correlationId} Exception during configuration initialization.",
67+
HttpContextExtensions.GetLoggerCorrelationId(HttpContext));
6268
}
6369

6470
return BadRequest();
@@ -92,12 +98,17 @@ public async Task<ActionResult> Index([FromBody] ConfigurationPostParameters con
9298
}
9399
else
94100
{
95-
_logger.LogError($"Failed to initialize configuration.");
101+
_logger.LogError(
102+
message: "{correlationId} Failed to initialize configuration.",
103+
HttpContextExtensions.GetLoggerCorrelationId(HttpContext));
96104
}
97105
}
98106
catch (Exception e)
99107
{
100-
_logger.LogError($"Exception during configuration initialization. {e}");
108+
_logger.LogError(
109+
exception: e,
110+
message: "{correlationId} Exception during configuration initialization.",
111+
HttpContextExtensions.GetLoggerCorrelationId(HttpContext));
101112
}
102113

103114
return BadRequest();

src/Service/Controllers/RestController.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,21 @@ private async Task<IActionResult> HandleOperation(
246246
}
247247
catch (DataApiBuilderException ex)
248248
{
249-
_logger.LogError($"{HttpContextExtensions.GetLoggerCorrelationId(HttpContext)}{ex.Message}");
250-
_logger.LogError($"{HttpContextExtensions.GetLoggerCorrelationId(HttpContext)}{ex.StackTrace}");
249+
_logger.LogError(
250+
exception: ex,
251+
message: "{correlationId} Error handling REST request.",
252+
HttpContextExtensions.GetLoggerCorrelationId(HttpContext));
253+
251254
Response.StatusCode = (int)ex.StatusCode;
252255
return ErrorResponse(ex.SubStatusCode.ToString(), ex.Message, ex.StatusCode);
253256
}
254257
catch (Exception ex)
255258
{
256-
_logger.LogError($"{HttpContextExtensions.GetLoggerCorrelationId(HttpContext)}{ex.Message}");
257-
_logger.LogError($"{HttpContextExtensions.GetLoggerCorrelationId(HttpContext)}{ex.StackTrace}");
259+
_logger.LogError(
260+
exception: ex,
261+
message: "{correlationId} Internal server error occured during REST request processing.",
262+
HttpContextExtensions.GetLoggerCorrelationId(HttpContext));
263+
258264
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
259265
return ErrorResponse(
260266
DataApiBuilderException.SubStatusCodes.UnexpectedError.ToString(),
@@ -277,9 +283,10 @@ private EntityActionOperation DeterminePatchPutSemantics(EntityActionOperation o
277283
{
278284
if (!string.Equals(HttpContext.Request.Headers["If-Match"], "*"))
279285
{
280-
throw new DataApiBuilderException(message: "Etags not supported, use '*'",
281-
statusCode: HttpStatusCode.BadRequest,
282-
subStatusCode: DataApiBuilderException.SubStatusCodes.BadRequest);
286+
throw new DataApiBuilderException(
287+
message: "Etags not supported, use '*'",
288+
statusCode: HttpStatusCode.BadRequest,
289+
subStatusCode: DataApiBuilderException.SubStatusCodes.BadRequest);
283290
}
284291

285292
switch (operation)

src/Service/Startup.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,13 @@ private void AddGraphQLService(IServiceCollection services)
255255
{
256256
if (error.Exception is not null)
257257
{
258-
_logger.LogError(error.Exception.Message);
259-
_logger.LogError(error.Exception.StackTrace);
258+
_logger.LogError(exception: error.Exception, message: "A GraphQL request execution error occurred.");
260259
return error.WithMessage(error.Exception.Message);
261260
}
262261

263262
if (error.Code is not null)
264263
{
265-
_logger.LogError(error.Code);
266-
_logger.LogError(error.Message);
264+
_logger.LogError(message: "Error code: {errorCode}\nError message: {errorMessage}", error.Code, error.Message);
267265
return error.WithMessage(error.Message);
268266
}
269267

@@ -592,7 +590,7 @@ private async Task<bool> PerformOnConfigChangeAsync(IApplicationBuilder app)
592590

593591
if (graphQLSchemaCreator is null || restService is null)
594592
{
595-
_logger.LogError($"Endpoint service initialization failed");
593+
_logger.LogError("Endpoint service initialization failed.");
596594
}
597595

598596
if (runtimeConfig.Runtime.Host.Mode == HostMode.Development)
@@ -614,15 +612,15 @@ private async Task<bool> PerformOnConfigChangeAsync(IApplicationBuilder app)
614612
}
615613
catch (DataApiBuilderException dabException)
616614
{
617-
_logger.LogError($"{dabException.Message}");
615+
_logger.LogError(exception: dabException, message: "OpenAPI Documentor initialization failed.");
618616
}
619617

620-
_logger.LogInformation($"Successfully completed runtime initialization.");
618+
_logger.LogInformation("Successfully completed runtime initialization.");
621619
return true;
622620
}
623621
catch (Exception ex)
624622
{
625-
_logger.LogError(ex, $"Unable to complete runtime initialization. Refer to exception for error details.");
623+
_logger.LogError(exception: ex, message: "Unable to complete runtime initialization. Refer to exception for error details.");
626624
return false;
627625
}
628626
}

0 commit comments

Comments
 (0)