Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

.NET: writing into event log

Posted on 2011-02-21
5
Medium Priority
?
514 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 500 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 500 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 500 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 500 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

824 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