Rob Smyth

Monday 11 November 2013

NLog MethodCallTarget configuration

I wanted to add an error indicator to a UI to give the user indication that a error or warning has been logged. I've found this very useful to get feedback from users. But each time I do this it takes me a while to get NLog to play nice with the application's XML logging configuration file. The thing I keep missing is to reload the configuration by:
NLog.LogManager.Configuration = loggingConfig;
Here is an example:


                var target = new NLog.Targets.MethodCallTarget();
                target.ClassName = this.GetType().AssemblyQualifiedName;
                target.MethodName = "OnErrorLogged";
                target.Parameters.Add(new MethodCallParameter("${level}"));
                target.Parameters.Add(new MethodCallParameter("${message}"));

                var loggingConfig = NLog.LogManager.Configuration;
                loggingConfig.AddTarget("UIErrorMonitor", target);

                var loggingRule = new LoggingRule("*", NLog.LogLevel.Error, target);

                NLog.LogManager.Configuration = loggingConfig;

        public static void OnErrorLogged(string level, string message)
// do stuff

No comments: