У меня есть NLog.Web.AspNetCore v6.0.5 в качестве зависимости. В program.cs я вызываю UseNLog с хоста:
Код: Выделить всё
var builder = WebApplication.CreateBuilder(args);
// NLog: Setup NLog for Dependency injection
builder.Logging.ClearProviders();
builder.Host.UseNLog();
Код: Выделить всё
private readonly ILogger _logger;
Код: Выделить всё
public MyController
(
IConfiguration configuration,
ILogger nlog,
IHttpClientFactory httpClientFactory
)
{
_configuration = configuration;
_logger = nlog;
_httpClientFactory = httpClientFactory;
}
Код: Выделить всё
//
if (!result.IsSuccessStatusCode)
{
string respContent = await result.Content.ReadAsStringAsync();
var errorObject = JsonConvert.DeserializeObject(respContent);
//
_logger.LogError("request error: {@errorObject}", errorObject);
//
}
Код: Выделить всё
nlog.configКод: Выделить всё
Код: Выделить всё
2025-11-26 13:22:00.5577|0|ERROR|Client.Controllers.MyController|request error: "Client.Models.ErrorResponses.APIError"
Подробнее здесь: https://stackoverflow.com/questions/798 ... ct-as-json
Мобильная версия