2

Closed

GetCurrentClassLogger() - frame.GetMethod().DeclaringType is null

description

Component: NLog
Impact: High
Release: NLog 2.0
 
In my application I get an error when I'm trying to create a logger.
 
my app-code:
public static class Start
{
[STAThread]
public static void Main(string[] args)
{
    ...
    var logger = CommonLogManager.LogInstance.GetCurrentClassLogger();
    ...
}
}
 
public static class CommonLogManager
{
private const string CONFIG_FILE = "NLog.config";
private const string BIN_DIR = "bin";
 
public static readonly LogFactory LogInstance = new LogFactory(new XmlLoggingConfiguration(getNLogConfigFilePath()));

private static string getNLogConfigFilePath()
{
    try
    {
        var pathToExcecutable = Path.GetDirectoryName(Application.ExecutablePath);
        if (!String.IsNullOrEmpty(pathToExcecutable))
        {
            var config = Path.Combine(pathToExcecutable, CONFIG_FILE);
            if (!File.Exists(config))
            {
                var path = Path.Combine(pathToExcecutable, BIN_DIR);
                config = Path.Combine(path, CONFIG_FILE);
 
                if (!File.Exists(config))
                throw new FileNotFoundException("Die Konfigurationsdatei für den Logger wurde nicht gefunden.", config);
            }
                return config;
        }
 
        throw new Exception("Der Pfad der Anwendung konnte nicht ermittelt werden.");
    }
    catch (Exception ex)
    {
        throw new Exception("Fehler im Logger", ex);
    }
}
}
 
my modified NLog-code (in NLog.LogFactory | netfx35):
[MethodImpl(MethodImplOptions.NoInlining)]
public Logger GetCurrentClassLogger()
{

if SILVERLIGHT

var frame = new StackFrame(1);

else

var frame = new StackFrame(1, false);

endif

if (frame == null)
    throw new NullReferenceException("frame is null");

if (frame.GetMethod() == null)
    throw new NullReferenceException("frame.GetMethod() is null");

if (frame.GetMethod().DeclaringType == null)
    throw new NullReferenceException(frame.GetMethod().Name + "   frame.GetMethod().DeclaringType is null");
 
if (frame.GetMethod().DeclaringType.FullName == null)
    throw new NullReferenceException("frame.GetMethod().DeclaringType.FullName is null");
 
return this.GetLogger(frame.GetMethod().DeclaringType.FullName);
}
Closed Oct 6, 2012 at 8:57 PM by Xharze
Moved to https://github.com/NLog/NLog/issues/28

comments