You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Asp.Net Core洋葱架构中如何实现NLog?

Implementing NLog in ASP.NET Core with Onion Architecture

Great question! When working with Onion Architecture in ASP.NET Core, setting up NLog in a clean, maintainable way absolutely benefits from a layered approach—let’s walk through how to do this properly, including whether you need a dedicated project.

Do You Need a Dedicated Project for NLog?

Yes, absolutely. Here’s why:

  • Onion Architecture emphasizes dependency inversion—inner layers (domain, application) should depend on abstractions, not concrete implementations.
  • Logging is part of the infrastructure layer (the outermost layer of the onion), so encapsulating NLog in a separate infrastructure project keeps your core layers (domain, application) free from direct dependencies on NLog.
  • This setup makes it easy to swap out NLog for another logging library later without touching your core business logic.

Step-by-Step Implementation

1. Create a Dedicated Logging Infrastructure Project

Create a class library project (e.g., YourApp.Infrastructure.Logging)—this will house all NLog-specific code and configuration.

Install the required NuGet packages in this project:

Install-Package NLog.Web.AspNetCore
Install-Package Microsoft.Extensions.Logging

2. Encapsulate NLog Initialization

Add a static extension method to configure NLog and map it to .NET Core’s ILogger abstraction. This keeps the configuration centralized and reusable:

using Microsoft.Extensions.Logging;
using NLog.Web;

namespace YourApp.Infrastructure.Logging;

public static class NLogSetupExtensions
{
    public static ILoggingBuilder AddNLogIntegration(this ILoggingBuilder loggingBuilder)
    {
        // Load NLog configuration from the project's nlog.config file
        var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
        
        try
        {
            // Clear default logging providers (like Console, Debug) to prioritize NLog
            loggingBuilder.ClearProviders();
            // Add NLog as the logging provider
            loggingBuilder.AddNLog(logger);
            
            return loggingBuilder;
        }
        catch (Exception ex)
        {
            logger.Error(ex, "Failed to initialize NLog");
            throw;
        }
    }
}

Add an nlog.config file to this project (set its "Copy to Output Directory" property to "Copy if newer") with your desired logging rules. Example configuration:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true">

  <targets>
    <!-- Write logs to a file -->
    <target name="file" xsi:type="File" 
            fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${level} ${message} ${exception:format=ToString}" />
    <!-- Optional: Write to console -->
    <target name="console" xsi:type="Console" />
  </targets>

  <rules>
    <logger name="*" minlevel="Info" writeTo="file,console" />
  </rules>
</nlog>

3. Integrate with Your ASP.NET Core Web Project

Reference the YourApp.Infrastructure.Logging project in your ASP.NET Core Web project. Then configure logging in Program.cs:

using YourApp.Infrastructure.Logging;

var builder = WebApplication.CreateBuilder(args);

// Add NLog using our encapsulated extension method
builder.Logging.AddNLogIntegration();

// Register other services (application layer, domain layer, etc.)
builder.Services.AddApplicationServices(); // Example for your application layer

var app = builder.Build();

// Configure middleware pipeline...
app.UseHttpsRedirection();
app.UseAuthorization();

app.MapControllers();

app.Run();

4. Use Logging in Application/Domain Layers

Your application layer (and even domain layer, if needed) only needs to depend on the Microsoft.Extensions.Logging.Abstractions NuGet package. Inject ILogger<T> into your services:

using Microsoft.Extensions.Logging;

namespace YourApp.Application.Services;

public class OrderService : IOrderService
{
    private readonly ILogger<OrderService> _logger;

    public OrderService(ILogger<OrderService> logger)
    {
        _logger = logger;
    }

    public async Task CreateOrderAsync(OrderDto orderDto)
    {
        _logger.LogInformation("Creating order with ID: {OrderId}", orderDto.Id);
        
        // Business logic here...
        
        _logger.LogDebug("Order {OrderId} created successfully", orderDto.Id);
    }
}

Key Benefits of This Approach

  • Compliance with Onion Architecture: Core layers (domain, application) depend only on abstractions (ILogger), not the concrete NLog implementation.
  • Centralized Configuration: All NLog setup is in one place, making it easy to update logging rules or targets.
  • Reusability: The logging project can be referenced by other infrastructure components (e.g., background services, console apps) in your solution.

内容的提问来源于stack exchange,提问作者Dallas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:05:46