Solved

.NET: writing into event log

Posted on 2011-02-21
5
503 Views
Last Modified: 2013-12-17
The code below writes into event log. How to change Type from Information to Error.

public static void Main(string[] args) {

    // Create a trace listener for the event log.
    EventLogTraceListener myTraceListener = new EventLogTraceListener("myEventLogSource");

    // Add the event log trace listener to the collection.
    Trace.Listeners.Add(myTraceListener);

    // Write output to the event log.
    Trace.WriteLine("Test output");
 }


0
Comment
Question by:quasar_ee
5 Comments
 
LVL 9

Accepted Solution

by:
rawinnlnx9 earned 125 total points
ID: 34947736
EventLog log;
log = new EventLog();
            
log.Source = "ErrorLog_SOMELOCATION";
log.WriteEntry(err.Message, EventLogEntryType.Error);

Open in new window

0
 
LVL 15

Assisted Solution

by:jorge_toriz
jorge_toriz earned 125 total points
ID: 34948378
You can create your own trace listener like this one:

public class MyLogListener : TraceListener
{
    EventLog eventLog;
    EventLogEntryType entryType;
   
    public MyLogListener(string logName, EventLogEntryType entryType)
      {
        eventLog = new EventLog(logName);
        this.entryType = entryType;
      }
    public override void Write(string message)
    {
        eventLog.WriteEntry(message, entryType);
    }
    public override void WriteLine(string message)
    {
        eventLog.WriteEntry(message, entryType);
    }
}

And then use it as you use your EventLogTraceListener:

MyLogListener listener = new MyLogListener("ExpertsExchange", EventLogEntryType.Warning);
System.Diagnostics.Trace.Listeners.Clear();
System.Diagnostics.Trace.Listeners.Add(listener);
System.Diagnostics.Trace.Write("Testing MyLogListener");
0
 
LVL 16

Assisted Solution

by:Kalpesh Chhatrala
Kalpesh Chhatrala earned 125 total points
ID: 34948547
using System;
using System.Diagnostics;

namespace WriteToAnEventLog_csharp
{
	/// Summary description for Class1.
	class Class1
	{
		static void Main(string[] args)
		{
			string sSource;
			string sLog;
			string sEvent;

			sSource = "dotNET Sample App";
			sLog = "Application";
			sEvent = "Sample Event";

			if (!EventLog.SourceExists(sSource))
				EventLog.CreateEventSource(sSource,sLog);

			EventLog.WriteEntry(sSource,sEvent);
			EventLog.WriteEntry(sSource, sEvent,
				EventLogEntryType.Warning, 234);
		}
	}
}

Open in new window

0
 
LVL 7

Assisted Solution

by:kovilpattiBalu
kovilpattiBalu earned 125 total points
ID: 34950514
EventLog.WriteEntry("Sample App", e.ToString(), EventLogEntryType.Error)
0
 

Author Closing Comment

by:quasar_ee
ID: 35112159
thanx
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

13 Experts available now in Live!

Get 1:1 Help Now