Solved

Custom event log in C#

Posted on 2013-11-12
6
1,095 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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:…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

12 Experts available now in Live!

Get 1:1 Help Now