Solved

.NET: writing into event log

Posted on 2011-02-21
5
508 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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 …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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