locked
Implementation of nlog RRS feed

  • Question

  • User-1350042179 posted

    Hi

    I have the code:

    public static void Initialize(string nombre, string ruta)
            {
                var config = new LoggingConfiguration();
                var target = new FileTarget(nombre);
                target.FileName = string.Concat(ruta, "/", nombre, "_${shortdate}.txt");           
                target.Layout = string.Concat("===================================================================================================================================================================================",
                    "${newline}Hora: ${longdate}.${newline}Nivel: ${level:upperCase=true}.${newline}Mensaje: ${message}.${newline}Source: ${callsite:includeSourcePath=true}.${newline}Linea: ${callsite-linenumber}.${newline}Stacktrace: ${stacktrace:topFrames=10}.${newline}Exception: ${exception:format=ToString}${newline}${newline}");
                var rule = new LoggingRule("*"LogLevel.Debug, target);
                config.LoggingRules.Add(rule);
     
                LogManager.Configuration = config;
            }

    I don't know the meaning of: 

    ${level:upperCase=true}
    

    Besides, I am not sure about this line:

    var rule = new LoggingRule("*"LogLevel.Debug, target);
    
    I suppose that the meaning is: Nlog will register
    • Debug
    • Info
    • Warn
    • Error
    • Fatal
    I have another question:
    The aplication will register Debug information automatically or I have to create custom code.

    Monday, April 23, 2018 3:18 AM

All replies

  • User1724605321 posted

    Hi neoaguil17,

    ${level:upperCase=true}

    This is the default layout setting which configures how log messages are written to a NLog target , level will show with upperCase format.

    I suppose that the meaning is: Nlog will register

    This line defines logging rules through LoggingRule objects . 

    In order to configure NLog in code you must complete the following steps:

    1. Create a LoggingConfiguration object that will hold the configuration
    2. Create one or more targets (objects of classes inheriting from Target)
    3. Set the properties of the targets
    4. Define logging rules through LoggingRule objects and add them to the configuration's LoggingRules
    5. Activate the configuration by assigning the configuration object to LogManager.Configuration

    Please refer to Configuration API for more details .

    The aplication will register Debug information automatically or I have to create custom code.

    Please refer to the basic tutorial below for how to use NLog:

    https://github.com/nlog/nlog/wiki/Tutorial

    https://weblogs.asp.net/jhallal/configure-nlog-for-asp-net-mvc 

    https://github.com/nlog/nlog/wiki/Web-resources 

    Best Regards,

    Nan Yu

    Tuesday, April 24, 2018 2:28 AM