Solved

.NET: writing into event log

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

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Media.Imaging 1 18
How can i use linq for parsing string datetime to real datetime? 16 44
Add different cell to otherwise similiar row 4 39
Sql server insert 13 29
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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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