Separate rule for loggers, one for details, another for exceptions.

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Separate rule for loggers, one for details, another for exceptions.

clab
I want to apologize now if this has been write into another thread, (please see 'Logging Threshold per Logger') but the problem always exist using NLog ver 0.9.5.0.
I need to create a separate rule for my loggers, one for normal log details, another only for Exception logs. There are my targets:
I tried to swap order to attach rules, and result is always log into TestNLog_..._Exception.log.
Another thing : activating newly prepared configuration, the log will not be created; if I comment this row, the log (one, not two) will be created.

This is my demo code, it is simple, copy and paste into VS.NET and run it, please.

Thank's you again.

Claudio

---------------------------------------------------
using System;

#region NLog using
  using NLog;
  using NLog.Targets;
  using NLog.Win32.Targets;
#endregion

class TestNLog
{

  #region NLog logger definitions
  /// <summary>
  /// Log class definition
  /// </summary>
  static Logger logger = LogManager.GetCurrentClassLogger();

  /// <summary>
  /// Exception Log class definition
  /// </summary>
  static Logger loggerException = LogManager.GetLogger("ExceptionLogger");
  #endregion

  /// <summary>
  /// Set programmatically NLog target for logging with Reporting Services.
  /// I can't use typically configuration file for NLog (unfortunately)
  /// because this class must be called by Reporting Services layer,
  /// therefore I had very difficults to setting exact location of NLog config file.
  /// </summary>
  static void Main()
  {
    FileTarget target1 = new FileTarget();
    target1.Layout = "${longdate} ${threadid} ${level:uppercase=true} ${message}";
    target1.FileName = "C:\\LOG\\TestNLog_${date:format=yyyy-MM-dd}.log";
    target1.KeepFileOpen = false;
    target1.Encoding = "iso-8859-2";
    target1.Name = "file";
    NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target1, LogLevel.Debug);

    FileTarget target2 = new FileTarget();
    target2.Layout = "${longdate} ${threadid} EXCEPTION ${message}  ${exception:format=tostring}";
    target2.FileName = "C:\\LOG\\TestNLog_${date:format=yyyy-MM-dd}_Exception.log";
    target2.KeepFileOpen = false;
    target2.Encoding = "iso-8859-2";
    target2.Name = "exceptionfile";
    NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target2, LogLevel.Debug);

    NLog.Config.LoggingConfiguration config = new NLog.Config.LoggingConfiguration();
    config.AddTarget("file", target1);
    config.AddTarget("exceptionfile", target2);

    NLog.Config.LoggingRule ruleA = new NLog.Config.LoggingRule("*", LogLevel.Info, target1);
    NLog.Config.LoggingRule ruleB = new NLog.Config.LoggingRule("ExceptionLogger", target2);

    // ignore any further rules as soon as this one matches
    ruleB.Final = true;

    // the rules are processed in the order they are added to LoggingRules
    config.LoggingRules.Add(ruleA);
    config.LoggingRules.Add(ruleB);

    //LogManager.Configuration = config; // this makes the newly prepared configuration active

    logger.Debug("This is DEBUG message.");
    logger.Info("This is INFO message.");
    logger.Warn("This is WARNING message.");
    logger.Error("This is ERROR message.");

    try
    {
      throw new System.ApplicationException("This is my exception!");
    }
    catch (Exception ex)
    {
      loggerException.ErrorException(ex.Message, ex);
    }
  }
}
Reply | Threaded
Open this post in threaded view
|

Re: Separate rule for loggers, one for details, another for exceptions.

Jarek Kowalski
Administrator
Here's the corrected one:

------
using System;

#region NLog using
using NLog;
using NLog.Targets;
//using NLog.Win32.Targets;
#endregion

class TestNLog
{

    #region NLog logger definitions
    /// <summary>
    /// Log class definition
    /// </summary>
    static Logger logger = LogManager.GetCurrentClassLogger();

    /// <summary>
    /// Exception Log class definition
    /// </summary>
    static Logger loggerException = LogManager.GetLogger("ExceptionLogger");
    #endregion

    /// <summary>
    /// Set programmatically NLog target for logging with Reporting Services.
    /// I can't use typically configuration file for NLog (unfortunately)
    /// because this class must be called by Reporting Services layer,
    /// therefore I had very difficults to setting exact location of NLog config file.
    /// </summary>
    static void Main()
    {
        FileTarget target1 = new FileTarget();
        target1.Layout = "${longdate} ${threadid} ${level:uppercase=true} ${message}";
        target1.FileName = "C:\\LOG\\TestNLog_${date:format=yyyy-MM-dd}.log";
        target1.KeepFileOpen = false;
        target1.Encoding = "iso-8859-2";
        target1.Name = "file";
        //NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target1, LogLevel.Debug);

        FileTarget target2 = new FileTarget();
        target2.Layout = "${longdate} ${threadid} EXCEPTION ${message}  ${exception:format=tostring}";
        target2.FileName = "C:\\LOG\\TestNLog_${date:format=yyyy-MM-dd}_Exception.log";
        target2.KeepFileOpen = false;
        target2.Encoding = "iso-8859-2";
        target2.Name = "exceptionfile";
        //NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target2, LogLevel.Debug);

        NLog.Config.LoggingConfiguration config = new NLog.Config.LoggingConfiguration();
        config.AddTarget("file", target1);
        config.AddTarget("exceptionfile", target2);

        NLog.Config.LoggingRule ruleA = new NLog.Config.LoggingRule("*", LogLevel.Info, target1);
        NLog.Config.LoggingRule ruleB = new NLog.Config.LoggingRule("ExceptionLogger", LogLevel.Debug, target2);

        // ignore any further rules as soon as this one matches
        ruleB.Final = true;

        // the rules are processed in the order they are added to LoggingRules
        config.LoggingRules.Add(ruleB);
        config.LoggingRules.Add(ruleA);

        LogManager.Configuration = config; // this makes the newly prepared configuration active

        logger.Debug("This is DEBUG message.");
        logger.Info("This is INFO message.");
        logger.Warn("This is WARNING message.");
        logger.Error("This is ERROR message.");

        try
        {
            throw new System.ApplicationException("This is my exception!");
        }
        catch (Exception ex)
        {
            loggerException.ErrorException(ex.Message, ex);
        }
    }
}
------------

I found 3 issues:

1. SimpleConfigurator is for simple cases - just one target, you shouldn't call it twice as the second call will overwrite the results of the first one.

2. You didn't pass the level parameter to ruleB constructor. I changed this to have LogLevel.Debug as a threshold.

3. The order of rules should be:

        // the rules are processed in the order they are added to LoggingRules
        config.LoggingRules.Add(ruleB);
        config.LoggingRules.Add(ruleA);

Jarek
NLog Blog
Reply | Threaded
Open this post in threaded view
|

Re: Separate rule for loggers, one for details, another for exceptions.

clab
Thank's Jarek, but I specified one logger for normal info, another for only exceptions.
The result file is always one, and not two log files.

Bye

Claudio


Reply | Threaded
Open this post in threaded view
|

Re: Separate rule for loggers, one for details, another for exceptions.

Jarek Kowalski
Administrator
I'm getting two files here:

TestNLog_2006-04-18.log:
-------
2006-04-18 17:50:13.1589 3876 INFO This is INFO message.
2006-04-18 17:50:13.1689 3876 WARN This is WARNING message.
2006-04-18 17:50:13.1689 3876 ERROR This is ERROR message.
-------

TestNLog_2006-04-18_Exception.log:
---------
2006-04-18 17:50:13.1689 3876 EXCEPTION This is my exception!  System.ApplicationException: This is my exception!
   at TestNLog.Main() in d:\work\consoleapplication9\class1.cs:line 71
----------

Jarek
NLog Blog
Reply | Threaded
Open this post in threaded view
|

Re: Separate rule for loggers, one for details, another for exceptions.

clab
No, I'm not, why????
Effectively your result is my goal. Can be only NLog version different.
I use Nlog 0.9.5, I must download snapshot version.
Can you send me link for latest snapshot ?
I retry after download.
Thank's Jarek.

Claudio
Reply | Threaded
Open this post in threaded view
|

Re: Separate rule for loggers, one for details, another for exceptions.

Jarek Kowalski
Administrator
Reply | Threaded
Open this post in threaded view
|

Re: Separate rule for loggers, one for details, another for exceptions.

clab
You are right !
It's perfect, it's work as I desired.

With my compliments, Jarek.

Ciao

Claudio