/// <summary> /// Configure logger by MethodCallTarget /// </summary> /// <param name="target"></param> public void configure(MethodCallTarget target) { this.target = target; lock(_lock) { LogManager.ConfigurationChanged -= onCfgLoggerChanged; LogManager.ConfigurationChanged += onCfgLoggerChanged; initLoggerCfg(); } Log.Trace(String.Format("Log('{0}') is configured for: '{1}'", target.ClassName, GuidList.PACKAGE_LOGGER)); }
/// <summary> /// Configure logger by default. /// </summary> public void configure() { var t = new MethodCallTarget() { ClassName = typeof(Log).AssemblyQualifiedName, MethodName = "nprint" }; t.Parameters.Add(new MethodCallParameter("${level:uppercase=true}")); t.Parameters.Add(new MethodCallParameter("${message}")); t.Parameters.Add(new MethodCallParameter("${ticks}")); configure(t); }
static void Main(string[] args) { MethodCallTarget target = new MethodCallTarget(); target.ClassName = typeof(Example).AssemblyQualifiedName; target.MethodName = "LogMethod"; target.Parameters.Add(new MethodCallParameter("${level}")); target.Parameters.Add(new MethodCallParameter("${message}")); NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug); Logger logger = LogManager.GetLogger("Example"); logger.Debug("log message"); logger.Error("error message"); }
// ReSharper disable once UnusedMethodReturnValue.Global public static IGlobalConfiguration UseNLogJobLogs(this IGlobalConfiguration configuration) { configuration.UseFilter(new JobLogContext()); MethodCallTarget target = new MethodCallTarget { ClassName = typeof (JobLogContext).AssemblyQualifiedName, MethodName = "Log", Parameters = { new MethodCallParameter("${level}"), new MethodCallParameter("${message}") } }; NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug); return configuration; }
private static void TestMethodCall(MethodCallRecord expected, string methodName, string className) { var target = new MethodCallTarget { Name = "t1", ClassName = className, MethodName = methodName }; target.Parameters.Add(new MethodCallParameter("param1", "test1")); target.Parameters.Add(new MethodCallParameter("param2", "2", typeof(int))); var configuration = new LoggingConfiguration(); configuration.AddTarget(target); configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, target)); LogManager.Configuration = configuration; LastCallTest = null; LogManager.GetCurrentClassLogger().Debug("test method 1"); Assert.Equal(expected, LastCallTest); }
private void ReplaceNLogConsoleLogger(ConsoleTarget consoleTarget) { // Construct new methodCall target. MethodCallTarget methodCallTarget = new MethodCallTarget(); methodCallTarget.ClassName = typeof(TerminalPluginInitializer).AssemblyQualifiedName; methodCallTarget.MethodName = "RedirectLogMessage"; methodCallTarget.Parameters.Add(new MethodCallParameter(consoleTarget.Layout)); // Add new methodCall target into the configuration. LogManager.Configuration.AddTarget("terminalLogger", methodCallTarget); // Reconfigure the logging rules using console target to use new methodCall target. foreach (LoggingRule rule in LogManager.Configuration.LoggingRules) { if (rule.Targets.Contains(consoleTarget)) { rule.Targets.Remove(consoleTarget); rule.Targets.Add(methodCallTarget); } } // Reconfigure existing loggers. LogManager.ReconfigExistingLoggers(); }
public static void Initialize(bool debugLog) { // final bet to be WinNT if (Environment.OSVersion.Platform != PlatformID.Win32NT) { throw new InvalidOperationException("Can not initialize windowed logger on non-Windows platforms."); } FreeConsole(); // create an event, start the thread, and wait for the thread to initialize ms_waitEvent = new ManualResetEvent(false); new Thread(UIThread).Start(); ms_waitEvent.WaitOne(); // initialize a logger for the thread var config = new LoggingConfiguration(); var target = new MethodCallTarget(); target.ClassName = typeof(WindowedLogger).AssemblyQualifiedName; target.MethodName = "LogOne"; string outputFormat; if (!debugLog) { outputFormat = "${level}: ${message}"; } else { outputFormat = "${mdc:item=sourceFile}(${mdc:item=sourceLine}): ${level}: ${mdc:item=typeName}::${mdc:item=memberName}: ${message}"; } target.Parameters.Add(new MethodCallParameter(outputFormat)); //NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Info); config.AddTarget("window", target); config.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, target)); LogManager.Configuration = config; LogManager.ThrowExceptions = true; // and dispose the wait event ms_waitEvent.Dispose(); }