Skip to content

Instantly share code, notes, and snippets.

@vcaraulean
Last active May 3, 2020 21:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vcaraulean/61a6899818b019b6ae17 to your computer and use it in GitHub Desktop.
Save vcaraulean/61a6899818b019b6ae17 to your computer and use it in GitHub Desktop.
Capture logging in LINQPad
// Configuring
// Open Query Properties (F4) for My Extensions
// 1. Additional References: add NLog package from nuget
// 2. Additional Namespace Imports: add NLog.Config & NLog
public static class NLogExtensions
{
public static void LogToResults(string loggerName = "*", string minLevel="Trace")
{
var nlogConfig = @"
<nlog>
<targets>
<target name='console' type='Console' layout='${date:format=dd/MM/yy HH\:mm\:ss\:fff} | ${level:uppercase=true} | ${message} | ${exception:format=tostring}' />
</targets>
<rules>
<logger name='{{0}}' minlevel='{{1}}' writeTo='console' />
</rules>
</nlog>
";
var parametrisedConfig = ConfigureLogger(nlogConfig, loggerName, minLevel);
using (var sr = new StringReader(parametrisedConfig))
{
using (var xr = XmlReader.Create(sr))
{
NLog.LogManager.Configuration = new XmlLoggingConfiguration(xr, null);
NLog.LogManager.ReconfigExistingLoggers();
}
}
}
private static string ConfigureLogger(string config, string loggerName, string minLevel)
{
return config
.Replace("{{0}}", loggerName)
.Replace("{{1}}", minLevel);
}
public static void LogToNLogViewer(string loggerName = "*", string minLevel="Trace")
{
var nlogConfig = @"
<nlog>
<targets>
<target name='log4view' type='NLogViewer' address='udp://127.0.0.1:878' />
</targets>
<rules>
<logger name='{{0}}' minlevel='{{1}}' writeTo='log4view' />
</rules>
</nlog>
";
var config = ConfigureLogger(nlogConfig, loggerName, minLevel);
using (var sr = new StringReader(config))
{
using (var xr = XmlReader.Create(sr))
{
NLog.LogManager.Configuration = new XmlLoggingConfiguration(xr, null);
NLog.LogManager.ReconfigExistingLoggers();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment