Skip to content

Commit cad4834

Browse files
author
Bart Koelman
committed
Tweaks in log formatting
1 parent 0d5b897 commit cad4834

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

src/JsonApiDotNetCore/Exceptions/InvalidRequestBodyException.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Net;
3-
using System.Text;
43
using JsonApiDotNetCore.Models.JsonApiDocuments;
54

65
namespace JsonApiDotNetCore.Exceptions
@@ -29,7 +28,7 @@ private static string FormatDetails(string details, string requestBody, Exceptio
2928
{
3029
if (text != null)
3130
{
32-
text += Environment.NewLine;
31+
text += " - ";
3332
}
3433

3534
text += "Request body: <<" + requestBody + ">>";

src/JsonApiDotNetCore/Exceptions/JsonApiException.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ namespace JsonApiDotNetCore.Exceptions
77
{
88
public class JsonApiException : Exception
99
{
10+
private static readonly JsonSerializerSettings _errorSerializerSettings = new JsonSerializerSettings
11+
{
12+
NullValueHandling = NullValueHandling.Ignore,
13+
Formatting = Formatting.Indented
14+
};
15+
1016
public Error Error { get; }
1117

1218
public JsonApiException(Error error, Exception innerException = null)
@@ -24,6 +30,6 @@ public JsonApiException(HttpStatusCode status, string message)
2430
};
2531
}
2632

27-
public override string Message => "Error = " + JsonConvert.SerializeObject(Error, Formatting.Indented);
33+
public override string Message => "Error = " + JsonConvert.SerializeObject(Error, _errorSerializerSettings);
2834
}
2935
}

src/JsonApiDotNetCore/Middleware/DefaultExceptionHandler.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ public DefaultExceptionHandler(ILoggerFactory loggerFactory, IJsonApiOptions opt
2121

2222
public ErrorDocument HandleException(Exception exception)
2323
{
24-
LogException(exception);
24+
Exception demystified = exception.Demystify();
25+
26+
LogException(demystified);
2527

26-
return CreateErrorDocument(exception);
28+
return CreateErrorDocument(demystified);
2729
}
2830

2931
private void LogException(Exception exception)
3032
{
3133
var level = GetLogLevel(exception);
32-
33-
Exception demystified = exception.Demystify();
34-
_logger.Log(level, demystified, $"Intercepted {demystified.GetType().Name}: {demystified.Message}");
34+
var message = GetLogMessage(exception);
35+
36+
_logger.Log(level, exception, message);
3537
}
3638

3739
protected virtual LogLevel GetLogLevel(Exception exception)
@@ -44,6 +46,13 @@ protected virtual LogLevel GetLogLevel(Exception exception)
4446
return LogLevel.Error;
4547
}
4648

49+
protected virtual string GetLogMessage(Exception exception)
50+
{
51+
return exception is JsonApiException jsonApiException
52+
? jsonApiException.Error.Title
53+
: exception.Message;
54+
}
55+
4756
protected virtual ErrorDocument CreateErrorDocument(Exception exception)
4857
{
4958
if (exception is InvalidModelStateException modelStateException)

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/UpdatingDataTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public async Task Response422IfUpdatingNotSettableAttribute()
8888
var error = document.Errors.Single();
8989
Assert.Equal(HttpStatusCode.UnprocessableEntity, error.StatusCode);
9090
Assert.Equal("Failed to deserialize request body.", error.Title);
91-
Assert.StartsWith("Property set method not found." + Environment.NewLine + "Request body: <<", error.Detail);
91+
Assert.StartsWith("Property set method not found. - Request body: <<", error.Detail);
9292
}
9393

9494
[Fact]

0 commit comments

Comments
 (0)