在 .NET CORE 3.1 Web API 项目中配置 nLog 的自定义布局渲染器
Configure Custom Layout Renderer of nLog in .NET CORE 3.1 Web API Project
我在我的 .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));
Program.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="..\logs\nlog-all-${shortdate}.log"
layout="${longdate}| ${hello-world}"> </target>
<target xsi:type="File" name="ownFile-web" fileName="..\logs\nlog-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>
LayoutRanderer class
[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!");
}
}
LoggerServiceClass
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);
}
}
网络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
我在我的 .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));
Program.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="..\logs\nlog-all-${shortdate}.log"
layout="${longdate}| ${hello-world}"> </target>
<target xsi:type="File" name="ownFile-web" fileName="..\logs\nlog-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>
LayoutRanderer class
[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!");
}
}
LoggerServiceClass
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);
}
}
网络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