AspNetTrace target
Writes log messages to the ASP.NET Trace using TraceContext from HttpContext.
Platforms Supported: Limited - .NET Framework only and requires nuget-package NLog.Web
<targets>
<target xsi:type="AspNetTrace" name="String" layout="Layout" />
</targets>
Read more about using the [Configuration File](Configuration file).
- name - Name of the target
-
layout - Layout used to format log messages. Layout Required. Default:
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
Log entries can then be viewed by navigating to http://server/path/Trace.axd.
AspNetTrace uses the HttpContext.Current
. Therefore async/buffered writing does not works for AspNetTrace due to different thread.
In order to use this target, put the following code in the [configuration file](Configuration file) such as Web.nlog or NLog.config:
<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="aspnet" xsi:type="ASPNetTrace" layout="${logger} ${message}" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="aspnet" />
</rules>
</nlog>
You can also configure the target programmatically, by putting the code in Application_Start event handler or similar:
using System;
using System.Web;
using NLog;
using NLog.Web.Targets;
namespace SomeWebApplication
{
public class Global : System.Web.HttpApplication
{
//
// this event handler is executed at the very start of the web application
// so this is a good place to configure targets programmatically
//
// alternative you could place this code in a static type constructor
//
protected void Application_Start(Object sender, EventArgs e)
{
NLog.Web.Targets.AspNetTraceTarget target = new NLog.Web.Targets.AspNetTraceTarget() { Name = "AspNetTrace" };
target.Layout = "${logger} ${message}";
NLog.Config.LoggingConfiguration config = new NLog.Config.LoggingConfiguration();
config.AddRuleForAllLevels(target)
NLog.LogManager.Configuration = config;
}
}
}
- Troubleshooting Guide - See available NLog Targets and Layouts: https://nlog-project.org/config
- Getting started
- How to use structured logging
- Troubleshooting
- FAQ
-
All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json