Solved

Custom event log in C#

Posted on 2013-11-12
6
1,192 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

734 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