EventLogTraceListener.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / Services / Monitoring / system / Diagnosticts / EventLogTraceListener.cs / 1 / EventLogTraceListener.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

/* 
 */ 
namespace System.Diagnostics {
    using System; 
    using System.Security;
    using System.Security.Permissions;
    using System.IO;
    using System.Text; 
    using System.Globalization;
    using System.Runtime.InteropServices; 
 
    /// 
    ///    Provides a simple listener for directing tracing or 
    ///       debugging output to a  or to a , such as  or
    ///    .
    /// 
    [HostProtection(SecurityAction.LinkDemand, Synchronization=true)] 
    public sealed class EventLogTraceListener : TraceListener {
        private EventLog eventLog; 
        private bool nameSet; 

        ///  
        /// Initializes a new instance of the  class without a trace
        ///    listener.
        /// 
        public EventLogTraceListener() { 
        }
 
        ///  
        /// Initializes a new instance of the  class using the
        ///    specified event log. 
        /// 
        public EventLogTraceListener(EventLog eventLog)
            : base((eventLog != null) ? eventLog.Source : string.Empty) {
            this.eventLog = eventLog; 
        }
 
        ///  
        /// Initializes a new instance of the  class using the
        ///    specified source. 
        /// 
        public EventLogTraceListener(string source) {
            eventLog = new EventLog();
            eventLog.Source = source; 
        }
 
        ///  
        ///    Gets or sets the event log to write to.
        ///  
        public EventLog EventLog {
            get {
                return eventLog;
            } 

            set { 
                eventLog = value; 
            }
        } 

        /// 
        ///     Gets or sets the
        ///       name of this trace listener. 
        /// 
        public override string Name { 
            get { 
                if (nameSet == false && eventLog != null) {
                    nameSet = true; 
                    base.Name = eventLog.Source;
                }

                return base.Name; 
            }
 
            set { 
                nameSet = true;
                base.Name = value; 
            }
        }

        ///  
        ///    Closes the text writer so that it no longer receives tracing or
        ///       debugging output. 
        ///  
        public override void Close() {
            if (eventLog != null) 
                eventLog.Close();
        }

        ///  
        /// 
        ///  
        protected override void Dispose(bool disposing) { 
            if (disposing)
                this.Close(); 
        }

        /// 
        ///    Writes a message to this instance's event log. 
        /// 
        public override void Write(string message) { 
            if (eventLog != null) eventLog.WriteEntry(message); 
        }
 
        /// 
        ///    Writes a message to this instance's event log followed by a line terminator.
        ///       The default line terminator is a carriage return followed by a line feed
        ///       (\r\n). 
        /// 
        public override void WriteLine(string message) { 
            Write(message); 
        }
 
        [
        ComVisible(false)
        ]
        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType severity, int id, 
                                                    string format, params object[] args)
        { 
            if (Filter != null && !Filter.ShouldTrace(eventCache, source, severity, id, format, args)) 
                return;
 
            EventInstance data = CreateEventInstance(severity, id);

            if (args == null) {
                eventLog.WriteEvent(data, format); 
            }
            else if(String.IsNullOrEmpty(format)) { 
                string[] strings = new string[args.Length]; 
                for (int i=0; i ushort.MaxValue) 
                id = ushort.MaxValue;
 
            // Ideally we need to pick a value other than '0' as zero is
            // a commonly used EventId by most applications
            if (id < ushort.MinValue)
                id = ushort.MinValue; 

            EventInstance data = new EventInstance(id, 0); 
 
            if (severity == TraceEventType.Error || severity == TraceEventType.Critical)
                data.EntryType = EventLogEntryType.Error; 
            else if (severity == TraceEventType.Warning)
                data.EntryType = EventLogEntryType.Warning;
            else
                data.EntryType = EventLogEntryType.Information; 

            return data; 
        } 

    } 
}



                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK