.NET: writing into event log

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.

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

Who is Participating?
rawinnlnx9Connect With a Mentor Commented:
EventLog log;
log = new EventLog();
log.Source = "ErrorLog_SOMELOCATION";
log.WriteEntry(err.Message, EventLogEntryType.Error);

Open in new window

jorge_torizConnect With a Mentor Research & Development ManagerCommented:
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.Write("Testing MyLogListener");
Kalpesh ChhatralaConnect With a Mentor Software ConsultantCommented:
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.WriteEntry(sSource, sEvent,
				EventLogEntryType.Warning, 234);

Open in new window

kovilpattiBaluConnect With a Mentor Commented:
EventLog.WriteEntry("Sample App", e.ToString(), EventLogEntryType.Error)
quasar_eeAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.