Odd Behavior when using a varialable for logger name

132 views
Skip to first unread message

Douglas Jones

unread,
May 17, 2014, 1:11:36 PM5/17/14
to nlog-...@googlegroups.com
In my never ending quest to create reusable libraries etc., I have been trying to figure out why when in my App.Config file I set the logger name to "${gdc:item=assemblyName}", I never get any logs. Yet if I use a a string that is the assemble name then it works! Can somebody explain this?


Here is my Logging Class:

namespace HandyTransmitter.Logging
{
    internal static class Log
    {
       public static Logger logger { get; private set; }
       static Log()
        {
            GlobalDiagnosticsContext.Set("assemblyName", GetAssemblyName());
            GlobalDiagnosticsContext.Set("version", GetCurrentBuild());
            logger = LogManager.GetLogger(GlobalDiagnosticsContext.Get("assemblyName"));
        }

        public static string GetCurrentBuild()
        {
            return Assembly.GetEntryAssembly().GetName().Version.ToString();
        }
        public static string GetAssemblyName()
        {
            return Assembly.GetEntryAssembly().GetName().Name;
        }
    }
}


The app.config that works:
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   
<variable name="appName" value="${gdc:item=assemblyName}" />
   
<variable name="HeaderLayout" value="********* ${processname} Assembly: ${gdc:item=assemblyName} Version: ${gdc:item=version} Windows ID: ${windows-identity} Started @ ${longdate} *********" />
   
<targets>
     
<target xsi:type="File" name="logfile" header="${HeaderLayout}" fileName="${cached:cached=true:inner=${specialfolder:CommonApplicationData}/${appName}/Logs/${shortdate}/${appName}${date:format=HHmmss}}.log" layout="${date:format=HH\:mm\:ss.ffff} ${uppercase:${level}} ${callsite}: ${message}" archiveFileName="${specialfolder:CommonApplicationData}/${appName}/Logs/archives/${appName}${shortdate}.{#}.log" archiveEvery="Day" archiveNumbering="Rolling" />
   
</targets>
   
<rules>
     
<logger name="HandyTransmitter" minlevel="Debug" writeTo="logfile" />
   
</rules>
 
</nlog>


And if I do this it will not create any logs:

  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   
<variable name="appName" value="${gdc:item=assemblyName}" />
   
<variable name="HeaderLayout" value="********* ${processname} Assembly: ${gdc:item=assemblyName} Version: ${gdc:item=version} Windows ID: ${windows-identity} Started @ ${longdate} *********" />
   
<targets>
     
<target xsi:type="File" name="logfile" header="${HeaderLayout}" fileName="${cached:cached=true:inner=${specialfolder:CommonApplicationData}/${appName}/Logs/${shortdate}/${appName}${date:format=HHmmss}}.log" layout="${date:format=HH\:mm\:ss.ffff} ${uppercase:${level}} ${callsite}: ${message}" archiveFileName="${specialfolder:CommonApplicationData}/${appName}/Logs/archives/${appName}${shortdate}.{#}.log" archiveEvery="Day" archiveNumbering="Rolling" />
   
</targets>
   
<rules>
     
<logger name="${appName}" minlevel="Debug" writeTo="logfile" />
   
</rules>
 
</nlog>


I am not seeing why. Timing? Something weird?

I am using Windows 7, WPF, .NET 4.5 and Nlog Version 2.1.0.0

Thanks!

Doug

Kim Christensen

unread,
May 23, 2014, 1:49:51 AM5/23/14
to nlog-...@googlegroups.com

Hi,

Could you provide the NLog debug output generated when running the application?

Reply all
Reply to author
Forward
0 new messages