Skip to content

Instantly share code, notes, and snippets.

@DForshner
Last active April 9, 2023 01:01
Show Gist options
  • Save DForshner/6095031 to your computer and use it in GitHub Desktop.
Save DForshner/6095031 to your computer and use it in GitHub Desktop.
Programmatically configure NLog
using NLog;
using NLog.Config;
using NLog.Targets;
using NLog.Targets.Wrappers;
using System.Diagnostics;
namespace Web.App_Start
{
/// <summary>
/// Programmatically configure NLog.
/// </summary>
public static class NLogConfig
{
/// <summary>
/// Configure Nlog. Normally called during Application_Start()
/// </summary>
/// <param name="connectionString">Connection string to logging database</param>
public static void Configure(string connectionString)
{
var config = new LoggingConfiguration();
ConfigureLogFile(config);
ConfigureNetLogViewer(config);
ConfigureLogDatabase(connectionString, config);
LogManager.Configuration = config;
LogManager.GetCurrentClassLogger().Info("NLogConfig - Configured.");
}
private static void ConfigureLogDatabase(string connectionString, LoggingConfiguration config)
{
var dbTarget = new NLog.Targets.DatabaseTarget();
dbTarget.ConnectionString = connectionString;
dbTarget.CommandText = @"insert into system_logging(log_date,log_level,log_message,log_user_name,log_ip_address, log_machine_name, log_computer_info, log_call_site, log_exception, log_stacktrace) values(@time_stamp, @level, @message, @user_name, @ipaddress, @machinename, @computer_info, @call_site, @log_exception, @stacktrace);";
dbTarget.Parameters.Add(new DatabaseParameterInfo("@time_stamp", new NLog.Layouts.SimpleLayout("${longdate}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@level", new NLog.Layouts.SimpleLayout("${level}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@message", new NLog.Layouts.SimpleLayout("${message}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@user_name", new NLog.Layouts.SimpleLayout("${aspnet-user-identity}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@ipaddress", new NLog.Layouts.SimpleLayout("${aspnet-request:serverVariable=REMOTE_ADDR}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@machinename", new NLog.Layouts.SimpleLayout("${machinename}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@computer_info", new NLog.Layouts.SimpleLayout("${aspnet-request:serverVariable=HTTP_USER_AGENT}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@call_site", new NLog.Layouts.SimpleLayout("${callsite:filename=false}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@log_exception", new NLog.Layouts.SimpleLayout("${exception}")));
dbTarget.Parameters.Add(new DatabaseParameterInfo("@stacktrace", new NLog.Layouts.SimpleLayout("${stacktrace:format=Raw}")));
var wrapper = new AsyncTargetWrapper();
wrapper.WrappedTarget = dbTarget;
wrapper.QueueLimit = 5000;
wrapper.OverflowAction = AsyncTargetWrapperOverflowAction.Discard;
config.AddTarget("database", wrapper);
var dbRule = new LoggingRule("*", LogLevel.Info, wrapper);
config.LoggingRules.Add(dbRule);
}
[Conditional("DEBUG")]
private static void ConfigureNetLogViewer(LoggingConfiguration config)
{
var netLogTarget = new NLog.Targets.NLogViewerTarget
{
Name = "netLogViewer",
Address = "udp4://127.0.0.1:7071",
IncludeMdc = true,
IncludeNLogData = true
};
var wrapper = new AsyncTargetWrapper();
wrapper.WrappedTarget = netLogTarget;
wrapper.QueueLimit = 5000;
wrapper.OverflowAction = AsyncTargetWrapperOverflowAction.Discard;
config.AddTarget("netLogView", wrapper);
var netLogRule = new LoggingRule("*", LogLevel.Debug, wrapper);
config.LoggingRules.Add(netLogRule);
}
private static void ConfigureLogFile(LoggingConfiguration config)
{
var fileTarget = new NLog.Targets.FileTarget
{
FileName = @"c:\logs\Debug.log",
Layout = @"${longdate} - ${level:uppercase=true}: ${message}${onexception:${newline}EXCEPTION\: ${exception:format=ToString}}",
KeepFileOpen = false,
ArchiveFileName = @"c:\logs\Debug_${shortdate}.{##}.log",
ArchiveNumbering = ArchiveNumberingMode.Sequence,
ArchiveEvery = FileArchivePeriod.Day
};
var wrapper = new AsyncTargetWrapper();
wrapper.WrappedTarget = fileTarget;
wrapper.QueueLimit = 5000;
wrapper.OverflowAction = AsyncTargetWrapperOverflowAction.Discard;
config.AddTarget("logfile", wrapper);
var fileRule = new LoggingRule("*", LogLevel.Info, wrapper);
config.LoggingRules.Add(fileRule);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment