0

I'm trying to log error objects as JSON in my ASP.NET Core 9 Web API. I am following the wiki in the NLog documentation: Structured logging.

I have NLog.Web.AspNetCore v6.0.5 as a dependency. In program.cs, I am calling UseNLog off of the Host:

var builder = WebApplication.CreateBuilder(args);

// NLog: Setup NLog for Dependency injection
builder.Logging.ClearProviders();
builder.Host.UseNLog();

In my controller I have ILogger as a class variable:

private readonly ILogger<MyController> _logger;

and assign it the injected ILogger in the constructor:

public MyController
(
    IConfiguration configuration, 
    ILogger<MyController> nlog, 
    IHttpClientFactory httpClientFactory
)
{
    _configuration = configuration;
    _logger = nlog;
    _httpClientFactory = httpClientFactory;
}

In the logger call I am using the @ prefix (the controller uses an HttpClient service and passes back any error).

//
if (!result.IsSuccessStatusCode)
{
    string respContent = await result.Content.ReadAsStringAsync();
    var errorObject = JsonConvert.DeserializeObject<APIError>(respContent);
//
    _logger.LogError("request error: {@errorObject}", errorObject);
//
}

nlog.config has the out-of-the-box layout:

<!-- File Target for own log messages with extra web details using some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="C:\Logs\client\web-${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId:whenEmpty=0}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />

But the log entry just outputs a call to ToString() on the APIError object instead of giving me the JSON representation:

2025-11-26 13:22:00.5577|0|ERROR|Client.Controllers.MyController|request error: "Client.Models.ErrorResponses.APIError"

What am I missing?

7
  • 1
    If you want to log respContent as a string, don't deserialize it. Commented Nov 27 at 1:40
  • @NeartCarp Upgrade to NLog v6.0.6 that fixes a bug in handling Newtonsoft JSON JObject. See also: github.com/NLog/NLog/pull/6030 Commented Nov 27 at 6:58
  • @StephenCleary this is true, the problem occurs throughout my code though, with objects that never have a string representation. Commented yesterday
  • @RolfKristensen after upgrading NLog, with the AspNetCore and Extensions.Logging packages, still calling ToString() Commented yesterday
  • @NeartCarp You are welcome to create an issue at github.com/NLog/NLog or github.com/NLog/NLog.Extensions.Logging with a small reproducible example. Then I will try to take a look. But please try calling the NLog Logger directly using NLog.LogManager.GetCurrentClassLogger().Error("@errorObject", errorObject) . Since Microsoft.Extension.Logging can intervene for IEnumerable-parameters (New behavior introduced with NET8 or NET9) Commented yesterday

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.