none
An object reference is required for the non-static field, method, or property

    Question

  • Hi

    I am writing a Dashboard type application that collects statistics for one of customer facing applications and displays the statistics to various stakeholders. The application is working fine, but when trying to log exceptions to the Event Log I am getting the following error:

    An object reference is required for the non-static field, method, or property

    I have a class defined as follows for the logging of the event, but when I build the solution, the error above is displayed. It is probably something really simple, but I just cannot figure it out.

    namespace Netbank.Dashboard.Website.MiddleTier.CAP 
        public class capDashboard 
        { 
            private void LogEvent(string Message, EventLogEntryType Level, int EventID) 
            { 
                String DashboardLog = "Dashboard"
                String DashboardLogSource = "Dashboard.Collection"
                EventLog dbdDashboardEventLog = new EventLog(DashboardLog); 
                dbdDashboardEventLog.Source = DashboardLogSource; 
     
                if (!EventLog.SourceExists(DashboardLog)) 
                { 
                    EventLog.CreateEventSource(DashboardLogSource, DashboardLog); 
                    dbdDashboardEventLog.WriteEntry(Message, Level, EventID); 
                } 
                else 
                { 
                    dbdDashboardEventLog.WriteEntry(Message, Level, EventID); 
                } 
            } 


    And then in the try block I have the code for the application, which is working 100% and in the catch block, I am writing the Event Log Entry as follows:

            public static Hashtable GetFieldData() 
            { 
                try 
                { 
     
                } 
     
                catch (Exception ex) 
                { 
                    LogEvent("Unable to Retrieve CAP data for the Main CAP Dashboard." + "\n" + "\n" + "Message:" + "\n" + "\n" + ex.Message + "\n" + "\n" + "Stack Trace:" + "\n" + "\n" + ex.StackTrace, EventLogEntryType.Error, 29953); 
                } 


    Systems Analyst
    Wednesday, January 21, 2009 7:26 PM

Answers

  • A simple solution would be to make LogEvent static (private static void LogEvent...).

    Although it seems to me that it would be better to take the logging outside the capDashboard class with and create Singleton for this purpose.

    Hope this helps.
    • Marked as answer by Barry Tandy Thursday, January 22, 2009 5:03 AM
    Wednesday, January 21, 2009 7:55 PM

All replies

  • A simple solution would be to make LogEvent static (private static void LogEvent...).

    Although it seems to me that it would be better to take the logging outside the capDashboard class with and create Singleton for this purpose.

    Hope this helps.
    • Marked as answer by Barry Tandy Thursday, January 22, 2009 5:03 AM
    Wednesday, January 21, 2009 7:55 PM
  • Thanks Fernando. That helped me too.
    GauranG
    Wednesday, July 06, 2011 7:32 AM