473,387 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why is inccorect Static Method called?

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;
}
}
Nov 16 '05 #1
1 1723
Greif <Gr***@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steven D'Aprano | last post by:
I've been doing a lot of reading about static methods in Python, and I'm not exactly sure what they are useful for or why they were introduced. Here is a typical description of them, this one...
1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
2
by: Tony Johansson | last post by:
Hello Experts! I know that you can't have virtual static methods and I know what a static method is. A static method exist only one time no matter how many object you have. You have this...
6
by: Dolphin White | last post by:
For example, I allocate some unmanaged resources in the static constructors, then how can I properly release the resource before the application exit? thx!
3
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
2
by: Tony Maresca | last post by:
Don't know if there are any Delphi/Object Pascal converts here, but being one, I sorely miss the feature that permits class (e.g., static) methods to be virtual and be overridden in derived...
2
by: superseed | last post by:
Hi, I'm pretty new to C#, and I'm quite stuck on the following problem. I would like to add to my application a Windows.Form (singleton) on which I could display a message of one of the...
3
by: Mike - EMAIL IGNORED | last post by:
MyClass { //I have a static member method: void static myMethod(); //and a static data member: static MyType myData; }; //In the .cpp file: void MyClass::myMethod()
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.