Solved

Custom event log in C#

Posted on 2013-11-12
6
1,171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

737 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