在.NET CORE 3.1 Web API项目中配置日志的自定义布局呈现器



我已经在.NET CORE 3.1 Web API项目中配置了nLog,一切正常。我想在nLog中使用自定义布局渲染器${hello-world},但无法在日志文件中查看结果。不确定我在这个谜题中遗漏了什么。https://nlog-project.org/2015/06/30/extending-nlog-is-easy.html

我的代码中需要以下配置吗?

ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("hello-world", typeof(HelloWorldLayoutRenderer));

程序.cs

public static void Main(string[] args)
{
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
try
{
logger.Debug("init main");
CreateHostBuilder(args).Build().Run();
}
catch (Exception exception)
{
logger.Error(exception, "Stopped program because of exception");
throw;
}
finally
{
NLog.LogManager.Shutdown();
}    
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureLogging((hostingContext, logging) =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseNLog();
}

服务配置

services.AddSingleton<ILoggerService, LoggerService>();

DI

containerBuilder.RegisterType<LoggerService>().As<ILoggerService>();

NLog.Config

<target xsi:type="File" name="allfile" fileName="..logsnlog-all-${shortdate}.log"
layout="${longdate}| ${hello-world}"> </target>
<target xsi:type="File" name="ownFile-web" fileName="..logsnlog-own-${shortdate}.log"
layout="${longdate}| ${environment: USERNAME}| ${hello-world}"></target>
<rules>
<logger name="*" minlevel="Trace" writeTo="allfile" />
<logger name="Microsoft.*" maxlevel="Info" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />    
</rules>

布局Randerer类

[LayoutRenderer("hello-world")]
public class HelloWorldLayoutRenderer : LayoutRenderer
{
public string Config1 { get; set; }
[RequiredParameter]
public string Config2 { get; set; }
[DefaultParameter]
public bool Caps { get; set; }
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
builder.Append("Hello World!");
}
}

LoggerService类

public class LoggerService: ILoggerService
{
private static ILogger logger = LogManager.GetCurrentClassLogger();
private const string LoggerName = "NLogLogger";
public void LogDebug(string message)
{
logger.Debug(message);
}
public void LogError(string message)
{
logger.Error(message);
}
public void LogInfo(string message)
{
logger.Info(message);
}
public void LogWarn(string message)
{
logger.Warn(message);
}
}

Web API控制器

[ApiController]
public class DashboardController : ControllerBase
{
private readonly ILoggerService Logger;
public DashboardController(
ILoggerService _logger
)
{
Logger = _logger;
Logger.LogDebug("NLog injected into HomeController.....");
}
/// test logs 
[HttpGet]
public ActionResult<HelloMessage> GetMessage()
{
Logger.LogInfo("Here is info message from the controller.");
Logger.LogDebug("Here is debug message from the controller.");
Logger.LogWarn("Here is warn message from the controller.");
Logger.LogError("Here is error message from the controller.");
}

不要这样做:

var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

然后尝试这样做:

var logger = NLog.LogManager.Setup()
.SetupExtensions(s => s.AutoLoadAssemblies(false).RegisterLayoutRenderer<HelloWorldLayoutRenderer>("hello-world"))
.RegisterNLogWeb()
.LoadConfigurationFromFile("nlog.config")
.GetCurrentClassLogger();

需要NLog.AspNetCore.Web版本4.9.3

另请参阅:https://nlog-project.org/2020/03/28/nlog-4-7-has-been-released.html

相关内容

  • 没有找到相关文章

最新更新