Click here to Skip to main content
15,887,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends
I am writing a sample application in which i am trying to write some messages in event viewer.
Suppose some other application has already created category name "ZONAL" under Applications and services log.I wanty to write my message in "ZONAL".
my code is here.
void eventLogging()
{
 
  // Create the source, if it does not already exist.
  String^ SOURCENAME = "ZONAL";
  String^ LOGNAME = "ZONAL";
  if(!EventLog::SourceExists(SOURCENAME))
  {
    EventLog::CreateEventSource(SOURCENAME, LOGNAME);
  }
  // Create an EventLog instance and assign its source.
   EventLog^ myLog = gcnew EventLog();
   myLog->Source = SOURCENAME;
 
  // Write an informational entry to the event log.    
   myLog->WriteEntry("Writing to event log from console app 2008");

}


int main(array<system::string xmlns:system="#unknown"> ^args)
{
    eventLogging();
    return 0;
}
</system::string>


After compiling when i run the code meesage is shown in Windows logs->Application but not in category in which i wanted.
Please tell if i am wrong somewhere or i am lacking any specific concept of event logging.

Thanks
Posted

1 solution

You write your logs in the system log not in "log viewer".

The concept of category is hard to understand. Please see my code where I created a wrapper designed to categorize the logs; in this approach you can have a separate folder for your product featuring more then one application. The logs for your product will go in the same separate folder, but each application will be categorized. It is good to work this way as you can see the order of logs from different applications if they communicate.

Here is the code: How to create event log under a folder[^].

Sorry, this is C#, but you won't have any problems understanding it.
If your goal is somewhat different, it does not matter: there are only two levels of hierarchy anyway.

—SA
 
Share this answer
 
Comments
kaushik4study 8-Jul-11 5:34am    
First of all thank you for answer.
I have read your code and it is a nice example.As per your code you are writing the message in a folder
string Application = "Event Log Test";
string EventLogName = "CodeProject";
you are right i am getting my messages in folder named "Event Log Test".
And under the source column "Event Log Test" displayed.
I do have one question.
Q1. Is it possible i can have different name say "Zonal" in source column.
Please tell me if possible.
Thank you for your quick answer
kaushik4study 8-Jul-11 6:36am    
Sorry One more thing.
In your code you have used two statements

1. EventLog log = new EventLog(EventLogName);//class EventLogInstallationHelper
2. EventLog eventLog = new EventLog();//usage part
eventLog.Source = Application;
What is the difference between both statement as we can see both are creating object of type EventLog. Is it somewhat related to my above question
Sergey Alexandrovich Kryukov 12-Jul-11 15:22pm    
Yes. First sample install and uninstall log. Think of installer or installation feature embedded in your application. When you want to remove your application from disk what do you do? If you self-uninstall it every time, you won't be able to see the logs in proper place. This is done once in the lifetime of installation of you program.

Second sample is periodic usage by application when a log is installed. Alternative is the static call (I think, I've shown it) which does not require any instantiation.
--SA
munazza.farooq 17-Mar-12 11:11am    
How to Log in seperate folder ?
Sergey Alexandrovich Kryukov 18-Mar-12 14:56pm    
I think this is a question you mentioned in your comment to my answer referenced above. I think I answered it in full in that solution; and I also tested it before posting. So, what difficulty do you have? Please explain.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900