Solved

Custom event log in C#

Posted on 2013-11-12
6
1,117 Views
Last Modified: 2013-11-14
Hi Experts,

I have below piece of code in c# and it's trying to create custom event log ,event source if it does not exist in the event viewer.

Now I don't want custom event log instead  it should write entries into "Application" category (log) in the event viewer.So in the below code, if myresponse is null then it should write error msgs to "Application" category  (with no custom event source ).I know by default, Windows Event Viewer places  log error messages generated by applications into "Application" log. In the below code ,how should I achieve ?


 if (myresppnse == null)
            {
                if(!EventLog.SourceExists("Demo"))

                {
                    EventLog.CreateEventSource("Demo", "Mylog");
                }

                EventLog.WriteEntry("Demo", "Error occured");

            }

Open in new window


Thanks in Advance
0
Comment
Question by:ksd123
  • 3
  • 3
6 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 200 total points
ID: 39641720
If i understand you correctly, and you just want to write to the "Application" log, then you just need to change the line:
EventLog.CreateEventSource("Demo", "Mylog");

Open in new window

To:
EventLog.CreateEventSource("Demo", "Application")

Open in new window

You can only create your source on one log, so if you create it on the Application log then that is where any event targeting your "Demo" source will go.
0
 

Author Comment

by:ksd123
ID: 39644242
I want to get rid of "source" (Demo) .That means "source" should determine by the system. In this case, I have defined it explicitly.

Any idea how can I do this?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39644342
As far as I am aware you have to specify a Source when writing events to the log. You can call it whatever you like, or use something that is already in the log, but you do need to specify something.

If you don't want to pass the source every time you write an entry then you can create an instance of the EventLog class rather than using the static methods (as demonstated in the example here: http://msdn.microsoft.com/en-us/library/xzwc042w(v=vs.110).aspx)
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:ksd123
ID: 39644428
Thank you for the quick response.In the msdn article we have below code and when it executes first time, will it create new source "MySource" with error details "Writing to event log" in the "Application" log ?


// Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "MySource";

        // Write an informational entry to the event log.    
        myLog.WriteEntry("Writing to event log.");
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39644551
As long as your application has permission to create a source and write a log entry, then yes, the code should create the source the first time you write a message.
0
 

Author Comment

by:ksd123
ID: 39647422
Thank you for the explanation
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now