public static void Main(string[] args) { AspNetLayoutRendererBase.Register("TR_ID", (logEventInfo, httpContext, loggingConfiguration) => { StringValues traceValues; if (httpContext == null) { return(null); } if (!httpContext.Request.Headers.TryGetValue("TR_ID", out traceValues)) { return(null); } return(traceValues.FirstOrDefault()); }); var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); try { logger.Debug("init main"); CreateHostBuilder(args).Build().Run(); } catch (Exception exception) { //NLog: catch setup errors logger.Error(exception, "Stopped program because of exception"); throw; } finally { // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux) NLog.LogManager.Shutdown(); } }
public void WithNLogger(Action <LoggerConfig> config) { // 自定义 layout renderer 一定要在加载 nlog.config 之前 // 详情见 issue#4014:https://github.com/NLog/NLog/issues/4014#issuecomment-644997803 var loggerConfig = new LoggerConfig(); config?.Invoke(loggerConfig); AspNetLayoutRendererBase.Register("NetAddress", (_, _, _) => loggerConfig.NetAddress); AspNetLayoutRendererBase.Register("LogLevel", (_, _, _) => loggerConfig.LogLevel); RegisterLayoutRenderer(); _services.AddSingleton(NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger()); }
private static void RegisterLayoutRenderer() { AspNetLayoutRendererBase.Register("requestId", (logInfo, context, cfg) => getHeaders(context, "requestId")); AspNetLayoutRendererBase.Register("platformId", (logInfo, context, cfg) => getHeaders(context, "platformId")); AspNetLayoutRendererBase.Register("userflag", (logInfo, context, cfg) => getHeaders(context, "userflag")); LayoutRenderer.Register <RequestDurationLayoutRenderer>("RequestDuration"); LayoutRenderer.Register <YearLayoutRenderer>("Year"); LayoutRenderer.Register <MonthLayoutRenderer>("Month"); LayoutRenderer.Register <HoursLayoutRenderer>("Hours"); string getHeaders(HttpContext context, string key) { if (context == null) { return(default);
public void RegisterLayoutRendererTest() { var httpcontextMock = SetupHttpAccessorWithHttpContext(); #if ASP_NET_CORE httpcontextMock.Connection.LocalPort.Returns(123); #else httpcontextMock.Request.RawUrl.Returns("123"); #endif // Act AspNetLayoutRendererBase.Register("test-web", (logEventInfo, httpContext, loggingConfiguration) => #if ASP_NET_CORE httpContext.Connection.LocalPort); #else httpContext.Request.RawUrl); #endif Layout l = "${test-web}"; var restult = l.Render(LogEventInfo.CreateNullEvent()); // Assert Assert.Equal("123", restult); }
/// <summary> /// Register a custom layout renderer using custom delegate-method <paramref name="layoutMethod" /> /// </summary> /// <param name="setupBuilder">Fluent style</param> /// <param name="name">Name of the layout renderer - without ${}.</param> /// <param name="layoutMethod">Delegate method that returns layout renderer output.</param> public static ISetupExtensionsBuilder RegisterAspNetLayoutRenderer(this ISetupExtensionsBuilder setupBuilder, string name, Func <LogEventInfo, HttpContextBase, LoggingConfiguration, object> layoutMethod) { AspNetLayoutRendererBase.Register(name, layoutMethod); return(setupBuilder); }