473,734 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ExceptionToLo g;
private CallerInfo m_CallerInfo;
private LogLevel m_LogLevelToLog ;

static LogHelper()
{
m_Log = LogManager.GetL ogger("appLogge r");
}

private LogHelper(objec t message, CallerInfo callerInfo, LogLevel level)
{
this.m_MessageT oLog = message;
this.m_LogLevel ToLog = level;
this.m_CallerIn fo = callerInfo;
}

private LogHelper(objec t message, Exception e, CallerInfo callerInfo,
LogLevel level)
{
this.m_MessageT oLog = message;
this.m_LogLevel ToLog = level;
this.m_Exceptio nToLog = e;
this.m_CallerIn fo = 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.G etFileName(),
sf.GetMethod(). ReflectedType.N ame, sf.GetMethod(). Name,
sf.GetFileLineN umber(), sf.GetFileColum nNumber());

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

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

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

// Set the thread to the background
loggerThread.Is Background = true;

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

private void writeToLog()
{
//Use Mapped Diagnostic Context to set the source information
MDC.Set(LOGGER_ FILE_NAME, m_CallerInfo.Fi leName);
MDC.Set(LOGGER_ CLASS_NAME, m_CallerInfo.Cl assName);
MDC.Set(LOGGER_ METHOD_NAME, m_CallerInfo.Me thodName);
MDC.Set(LOGGER_ LINE_NUMBER, m_CallerInfo.Li neNumber.ToStri ng());
MDC.Set(LOGGER_ COLUMN_NUMBER, m_CallerInfo.Co lumnNumber.ToSt ring());
if(!(m_Exceptio nToLog==null))
MDC.Set(LOGGER_ MESSAGE, m_ExceptionToLo g.Message);

switch(m_LogLev elToLog)
{
case LogLevel.DEBUG:
if (m_Log.IsDebugE nabled)
{
if (m_ExceptionToL og != null)
{
m_Log.Debug(m_M essageToLog, m_ExceptionToLo g);
}
else
{
m_Log.Debug(m_M essageToLog);
}
}
break;
case LogLevel.ERROR:
if (m_Log.IsErrorE nabled)
{
if (m_ExceptionToL og != null)
{
m_Log.Error(m_M essageToLog, m_ExceptionToLo g);
}
else
{
m_Log.Error(m_M essageToLog);
}
}
break;
case LogLevel.FATAL:
if (m_Log.IsFatalE nabled)
{
if (m_ExceptionToL og != null)
{
m_Log.Fatal(m_M essageToLog, m_ExceptionToLo g);
}
else
{
m_Log.Fatal(m_M essageToLog);
}
}
break;
case LogLevel.INFO:
if (m_Log.IsInfoEn abled)
{
if (m_ExceptionToL og != null)
{
m_Log.Info(m_Me ssageToLog, m_ExceptionToLo g);
}
else
{
m_Log.Info(m_Me ssageToLog);
}
}
break;
case LogLevel.WARN:
if (m_Log.IsWarnEn abled)
{
if (m_ExceptionToL og != null)
{
m_Log.Warn(m_Me ssageToLog, m_ExceptionToLo g);
}
else
{
m_Log.Warn(m_Me ssageToLog);
}
}
break;
default:
break;
}
}
Nov 16 '05 #1
1 1742
Greif <Gr***@discussi ons.microsoft.c om> 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.co m>
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
1996
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 from Guido: "The new descriptor API makes it possible to add static methods and class methods. Static methods are easy to describe: they behave pretty much like static methods in C++ or Java." http://www.python.org/2.2.3/descrintro.html
1
2207
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 allocated on the stack but that's not a real reason. Which is OK because this isn't a real question, it's just a complaint dressed up to look like a reason Second question. If a language doesn't support a fairly obvious feature, one has to wonder if...
12
13463
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 of your Derived classes will need to implement some method, but implement it differently, and that the base class cannot implement it. This is where pure virtual comes in.
2
3161
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 static method even if you have no objects at all. Static methods belongs only to the class and not to the object you have. They can only access static members.
6
6596
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
11830
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 add items to this list, but have the base class hold the data and expose the property. Problem is, however, that the derived class' static constructor doesn't get called when DerivedClass.MyList is accessed, so the list has no members. If
2
1738
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 classes. Related to that, given: public class Foo { static void SomeMethod()
2
2656
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 following type : Exception, Error, Warning, Infos (with differents colors, etc). I would like to use it like the Console.WriteLine("My Message"); on everywhere in my application.
3
6384
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
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.