Solved

Custom event log in C#

Posted on 2013-11-12
6
1,155 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why use this lambda? 12 60
C# Windows app updating JSON file error. 9 38
Securing WEBAPI on Azure 2 20
How to escape alias names for Oracle query 1 25
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

740 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