EventLogger.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / Tools / xws_reg / System / ServiceModel / Install / EventLogger.cs / 1 / EventLogger.cs

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

namespace System.ServiceModel.Install 
{
    using System; 
    using System.Diagnostics; 
    using System.Globalization;
    using System.ComponentModel; 

    internal static class EventLogger
    {
        const int MaxEventLogLength = 32768; 
        static MsiStyleLogWriter msiStyleLogWriter = null;
 
        static EventLogger() 
        {
            if (!EventLog.SourceExists(ServiceModelInstallStrings.EventLogSource)) 
            {
                EventLog.CreateEventSource(ServiceModelInstallStrings.EventLogSource, ServiceModelInstallStrings.EventLogName);
            }
        } 

        internal static void InitializeMsiStyleLogging() 
        { 
            EventLogger.msiStyleLogWriter = MsiStyleLogWriter.CreateWriter();
        } 

        internal static void TerminateMsiStyleLogging()
        {
            if (EventLogger.msiStyleLogWriter != null) 
            {
                DateTime now = DateTime.Now; 
                EventLogger.msiStyleLogWriter.WriteRaw(SR.GetString(SR.MsiStyleLogTerminator, now.ToShortDateString(), 
                    now.ToString("HH:mm:ss", CultureInfo.CurrentCulture)));
            } 

            EventLogger.msiStyleLogWriter = null;
        }
 
        internal static void LogException(Exception e)
        { 
            EventLogger.LogToConsole(SR.GetString(SR.Error, e.Message)); 
            EventLogger.WriteMsiStyleLogEntry(e.ToString());
            EventLogger.WriteLogEntry(e.ToString(), EventLogEntryType.Error); 
        }

        internal static void LogError(string message)
        { 
            EventLogger.LogToConsole(message);
            EventLogger.WriteMsiStyleLogEntry(SR.GetString(SR.Error, message)); 
            EventLogger.WriteLogEntry(message, EventLogEntryType.Error); 
        }
 
        internal static void LogInformation(string message, bool displayToConsole)
        {
            if (displayToConsole)
            { 
                EventLogger.LogToConsole(message);
            } 
 
            EventLogger.WriteMsiStyleLogEntry(SR.GetString(SR.Information, message));
            EventLogger.WriteLogEntry(message, EventLogEntryType.Information); 
        }

        internal static void LogToConsole(string message)
        { 
            Console.WriteLine();
            Console.WriteLine(message); 
        } 

        internal static void LogWarning(string message, bool displayToConsole) 
        {
            if (displayToConsole)
            {
                EventLogger.LogToConsole(message); 
            }
 
            EventLogger.WriteMsiStyleLogEntry(SR.GetString(SR.Warning, message)); 
            EventLogger.WriteLogEntry(message, EventLogEntryType.Warning);
        } 

        internal static void WriteLogEntry(string message, EventLogEntryType entryType)
        {
            try 
            {
                EventLog.WriteEntry(ServiceModelInstallStrings.EventLogSource, 
                    message.Trim(), entryType); 
            }
            catch (ArgumentException e) 
            {
                // Issues related to message length
                EventLogger.WriteMsiStyleLogEntry(SR.GetString(SR.ErrorWritingEventLogEntry, e.ToString()));
            } 
            catch (InvalidOperationException e)
            { 
                // Registry access issues 
                EventLogger.WriteMsiStyleLogEntry(SR.GetString(SR.ErrorWritingEventLogEntry, e.ToString()));
            } 
            catch (Win32Exception e)
            {
                // Other OS reported issues
                EventLogger.WriteMsiStyleLogEntry(SR.GetString(SR.ErrorWritingEventLogEntry, e.ToString())); 
            }
        } 
 
        internal static void WriteMsiStyleLogEntry(string message)
        { 
            if (EventLogger.msiStyleLogWriter != null)
            {
                EventLogger.msiStyleLogWriter.WriteEntry(message);
            } 
        }
    } 
} 


// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.


                        

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