Why is inccorect Static Method called?

G

Guest

I am trying to call a static method within a sealed class. However, the wrong
method keeps getting called instead.

In a seperate class I am calling the following code:
LogHelper.Log("Sample Exception", ex, LogLevel.ERROR);

Here is the weird thing, the public static void Log(string message, LogLevel
level) method is always called instead of the public static void Log(string
message, Exception e, LogLevel level) method. I don't know why. The method
signature is correct. I am stumped. Any help would be greatly appreciated.
Thanks.

Here is my class definition:

private static readonly ILog m_Log;
private object m_MessageToLog;
private Exception m_ExceptionToLog;
private CallerInfo m_CallerInfo;
private LogLevel m_LogLevelToLog;

static LogHelper()
{
m_Log = LogManager.GetLogger("appLogger");
}

private LogHelper(object message, CallerInfo callerInfo, LogLevel level)
{
this.m_MessageToLog = message;
this.m_LogLevelToLog = level;
this.m_CallerInfo = callerInfo;
}

private LogHelper(object message, Exception e, CallerInfo callerInfo,
LogLevel level)
{
this.m_MessageToLog = message;
this.m_LogLevelToLog = level;
this.m_ExceptionToLog = e;
this.m_CallerInfo = callerInfo;
}

public static void Log(string message, LogLevel level)
{
LogX(message, (Exception)null, level);
}

public static void Log(string message, Exception e, LogLevel level)
{
LogX(message, e, level);
}

private static void LogX(string message, Exception e, LogLevel level)
{
StackFrame sf;
CallerInfo callerInfo;
LogHelper logger;
Thread loggerThread;

// Grab the data from 2 frames up the stack (source application)
sf = new StackFrame(2, true);

// Populate the caller info
callerInfo = new CallerInfo(sf.GetFileName(),
sf.GetMethod().ReflectedType.Name, sf.GetMethod().Name,
sf.GetFileLineNumber(), sf.GetFileColumnNumber());

// Create an instance of the logger
logger = new LogHelper(message, e, callerInfo, level);

// Create the worker thread
loggerThread = new Thread(new ThreadStart(logger.writeToLog));

// Give the thread a name
loggerThread.Name = "Log4Net Worker Thread";

// Set the thread to the background
loggerThread.IsBackground = true;

// Start the thread
loggerThread.Start();
}

private void writeToLog()
{
//Use Mapped Diagnostic Context to set the source information
MDC.Set(LOGGER_FILE_NAME, m_CallerInfo.FileName);
MDC.Set(LOGGER_CLASS_NAME, m_CallerInfo.ClassName);
MDC.Set(LOGGER_METHOD_NAME, m_CallerInfo.MethodName);
MDC.Set(LOGGER_LINE_NUMBER, m_CallerInfo.LineNumber.ToString());
MDC.Set(LOGGER_COLUMN_NUMBER, m_CallerInfo.ColumnNumber.ToString());
if(!(m_ExceptionToLog==null))
MDC.Set(LOGGER_MESSAGE, m_ExceptionToLog.Message);

switch(m_LogLevelToLog)
{
case LogLevel.DEBUG:
if (m_Log.IsDebugEnabled)
{
if (m_ExceptionToLog != null)
{
m_Log.Debug(m_MessageToLog, m_ExceptionToLog);
}
else
{
m_Log.Debug(m_MessageToLog);
}
}
break;
case LogLevel.ERROR:
if (m_Log.IsErrorEnabled)
{
if (m_ExceptionToLog != null)
{
m_Log.Error(m_MessageToLog, m_ExceptionToLog);
}
else
{
m_Log.Error(m_MessageToLog);
}
}
break;
case LogLevel.FATAL:
if (m_Log.IsFatalEnabled)
{
if (m_ExceptionToLog != null)
{
m_Log.Fatal(m_MessageToLog, m_ExceptionToLog);
}
else
{
m_Log.Fatal(m_MessageToLog);
}
}
break;
case LogLevel.INFO:
if (m_Log.IsInfoEnabled)
{
if (m_ExceptionToLog != null)
{
m_Log.Info(m_MessageToLog, m_ExceptionToLog);
}
else
{
m_Log.Info(m_MessageToLog);
}
}
break;
case LogLevel.WARN:
if (m_Log.IsWarnEnabled)
{
if (m_ExceptionToLog != null)
{
m_Log.Warn(m_MessageToLog, m_ExceptionToLog);
}
else
{
m_Log.Warn(m_MessageToLog);
}
}
break;
default:
break;
}
}
 
J

Jon Skeet [C# MVP]

Greif said:
I am trying to call a static method within a sealed class. However, the wrong
method keeps getting called instead.

In a seperate class I am calling the following code:
LogHelper.Log("Sample Exception", ex, LogLevel.ERROR);

Here is the weird thing, the public static void Log(string message, LogLevel
level) method is always called instead of the public static void Log(string
message, Exception e, LogLevel level) method. I don't know why. The method
signature is correct. I am stumped. Any help would be greatly appreciated.

It sounds very unlikely, to be honest.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top