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

NLog自定义日志级别及config.json中MyEntry日志条目相关技术咨询

Answers to Your Logging & NLog Sandbox Questions

Let’s walk through each of your questions clearly:

1. What is the "MyEntry" entry you added?

That’s a custom log category you’ve defined in your .NET logging configuration. In .NET’s logging system, categories are named groups you can use to organize logs from different parts of your application—like a specific service, feature, or module. Think of it as a label that lets you group and filter logs related to a particular area of your code.

2. Is "MyEntry" a log category?

Absolutely! All those keys under LogLevel (like Default, Microsoft, and your MyEntry) are log category names. .NET lets you define any custom category string you want to segment your logs.

3. How to make "MyEntry" take effect?

To activate this configuration, you need to use the "MyEntry" category when writing logs in your code, and ensure your logging provider (NLog, in your case) respects the level setting. Here’s how:

Step 1: Use the category in code

When creating a logger, specify "MyEntry" as the category name. For example:

// Using ILoggerFactory
var myEntryLogger = loggerFactory.CreateLogger("MyEntry");
myEntryLogger.LogCritical("This critical log will be captured (matches your level setting)");
myEntryLogger.LogError("This error log will be filtered out (since MyEntry's level is set to Critical)");

// Or via dependency injection
private readonly ILogger _myEntryLogger;
public MyService(ILoggerFactory loggerFactory)
{
    _myEntryLogger = loggerFactory.CreateLogger("MyEntry");
}

Step 2: Configure NLog to recognize the category

In your NLog.config file, add a rule that targets the "MyEntry" category to ensure NLog applies your level filter. For example:

<rules>
  <!-- Send MyEntry's Critical logs to a specific file -->
  <logger name="MyEntry" minlevel="Critical" writeTo="myEntryLogFile" />
  
  <!-- Default rule for other logs -->
  <logger name="*" minlevel="Trace" writeTo="defaultFile" />
</rules>

This tells NLog to only process Critical-level logs from the "MyEntry" category and send them to your specified target (like a dedicated log file).

4. Setting up an NLog sandbox project to test configurations

Creating a sandbox is a great way to experiment with NLog’s features. Here’s a simple step-by-step guide:

  • Create a new project: Start with a .NET Console App (it’s lightweight and easy to iterate on).
  • Install NuGet packages: Add NLog.Extensions.Logging (for basic logging integration). If you want ASP.NET Core features, add NLog.Web.AspNetCore too.
  • Add NLog.config: Create an NLog.config file in your project root, and set its "Copy to Output Directory" property to "Copy if newer" (right-click the file → Properties → Advanced). A basic config might look like:
    <?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">
    
      <targets>
        <target name="console" xsi:type="Console" />
        <target name="myEntryFile" xsi:type="File" fileName="${basedir}/logs/myentry.log" />
        <target name="defaultFile" xsi:type="File" fileName="${basedir}/logs/default.log" />
      </targets>
    
      <rules>
        <logger name="MyEntry" minlevel="Critical" writeTo="myEntryFile,console" />
        <logger name="*" minlevel="Trace" writeTo="defaultFile,console" />
      </rules>
    </nlog>
    
  • Configure logging in Program.cs: For .NET 6+, set up NLog and test different logs:
    using NLog;
    
    var logger = LogManager.Setup().LoadConfigurationFromFile("NLog.config").GetCurrentClassLogger();
    
    try
    {
        logger.Info("Sandbox project started");
        
        // Test your custom MyEntry category
        var myEntryLogger = LogManager.GetLogger("MyEntry");
        myEntryLogger.Critical("Test Critical log for MyEntry");
        myEntryLogger.Error("This Error log should be filtered out");
        
        // Test other categories
        var defaultLogger = LogManager.GetLogger("Default");
        defaultLogger.Trace("This Trace log should show up (matches Default's level)");
    }
    catch (Exception ex)
    {
        logger.Error(ex, "An error occurred");
    }
    finally
    {
        LogManager.Shutdown();
    }
    
  • Experiment freely: Tweak your config.json LogLevel settings and NLog.config rules to see how logs are filtered, routed, and formatted. Try adding different targets (like database logging or email alerts) to explore NLog’s full capabilities.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 04:08:12