public static string GetSourceName(string applicationName)
        {
            if (LogEventSourceProvider._sources.ContainsKey(applicationName))
            {
                return(LogEventSourceProvider._sources[applicationName]);
            }

            if (LogEventSourceProvider.IsRegistered(applicationName))
            {
                LogEventSourceProvider._sources[applicationName] = applicationName;

                return(applicationName);
            }

            return(LogEventSourceProvider._sources[_defaultSource]);
        }
        private static bool IsRegistered(string sourceName)
        {
            var isExists = EventLog.SourceExists(sourceName);

            if (!isExists)
            {
                if (LogEventSourceProvider.IsUserAdministrator())
                {
                    var source = new EventSourceCreationData(sourceName, EventLogName);

                    EventLog.CreateEventSource(source);
                }
            }

            return(isExists);
        }
예제 #3
0
        /// <summary>
        /// Static method that tries to write an message in the event log and return true or false in case of success or failure respectively. Result object contains the details of the exception
        /// trown during the process, null if it succeeds
        /// </summary>
        /// provided by LogEventSourceProvider implementation</param>
        /// <param name="exception">An exeption to be logged. Construct string with messages and stack trace from the exception
        /// and all inner exceptions(message and stack trace included).</param>
        /// <param name="result">An object of type Exception with all exception thrown during the logging process.</param>
        /// <returns>True if the process succeeds, false otherwise. result object see result for details</returns>
        public static bool TryWriteToEventLog(string appId, string entry, EventLogEntryType eventLogEntryType, out Exception result)
        {
            result = null;

            try
            {
                var source = LogEventSourceProvider.GetSourceName(appId);

                var eventLog = new EventLog(LogEventSourceProvider.EventLogName);

                eventLog.Source = source;

                eventLog.WriteEntry(entry, eventLogEntryType);

                return(true);
            }
            catch (Exception ex)
            {
                result = ex;

                return(false);
            }
        }