Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

.NET: writing into event log

Posted on 2011-02-21
5
Medium Priority
?
513 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

661 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