Skip to content

Instantly share code, notes, and snippets.

@3F
Created September 2, 2015 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3F/6db7d78bb2c138393060 to your computer and use it in GitHub Desktop.
Save 3F/6db7d78bb2c138393060 to your computer and use it in GitHub Desktop.
Example of NLog initialization with bug fixes from other assemblies if exists
/*
* Example of NLog initialization with bug fixes from other assemblies if exists:
* 1. if another used the NLog.Config.SimpleConfigurator.
* 2. if target from another has been configured as "*" (optional bug)
*/
using NLog;
using NLog.Config;
using NLog.Filters;
using NLog.Targets;
....
/// <summary>
/// Filter for disabling logger
/// </summary>
protected ConditionBasedFilter selfLogger = new ConditionBasedFilter() {
Condition = String.Format("(logger=='{0}')", GuidList.PACKAGE_LOGGER),
Action = FilterResult.Ignore,
};
...
protected void configureLogger()
{
lock(_lock)
{
LogManager.ConfigurationChanged -= onCfgLoggerChanged;
LogManager.ConfigurationChanged += onCfgLoggerChanged;
initLoggerCfg();
}
}
/// <summary>
/// To disable our logger for other assemblies.
/// </summary>
/// <param name="cfg">Configuration of logger</param>
protected void fixLoggerCfg(LoggingConfiguration cfg)
{
fixLoggerCfg(cfg, selfLogger);
}
/// <summary>
/// To disable logger for other assemblies.
/// </summary>
/// <param name="cfg">Configuration of logger</param>
/// <param name="filter">Custom filter</param>
protected void fixLoggerCfg(LoggingConfiguration cfg, ConditionBasedFilter filter)
{
if(cfg == null) {
return;
}
LoggingRule rule = cfg.LoggingRules.FirstOrDefault(p => p.LoggerNamePattern == "*");
if(rule == null) {
return;
}
if(!rule.Filters.Contains(filter)) {
rule.Filters.Add(filter);
}
}
/// <summary>
/// Initialize logger
/// </summary>
private void initLoggerCfg()
{
LoggingConfiguration config = LogManager.Configuration;
if(config == null) {
config = new LoggingConfiguration();
}
NLog.Targets.Target t = config.FindTargetByName(GuidList.PACKAGE_LOGGER);
if(t != null) {
return; // the config is already contains our logger
}
// configure entry point:
MethodCallTarget target = new MethodCallTarget() {
ClassName = typeof(Log).AssemblyQualifiedName,
MethodName = "nprint"
};
target.Parameters.Add(new MethodCallParameter("${level:uppercase=true}"));
target.Parameters.Add(new MethodCallParameter("${message}"));
target.Parameters.Add(new MethodCallParameter("${ticks}"));
//
config.AddTarget(GuidList.PACKAGE_LOGGER, target);
config.LoggingRules.Add(new LoggingRule(GuidList.PACKAGE_LOGGER, LogLevel.Trace, target));
// update configuration
LogManager.Configuration = config;
LogManager.Configuration.Reload();
}
/// <summary>
/// Protects from NLog.Config.SimpleConfigurator from others assemblies.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void onCfgLoggerChanged(object sender, LoggingConfigurationChangedEventArgs e)
{
fixLoggerCfg(e.OldConfiguration); // if we are in point after first initialization
fixLoggerCfg(e.NewConfiguration); // if this is raised from others
initLoggerCfg(); // we also should be ready to SimpleConfigurator from other assemblies etc.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment