/// <summary>
        /// Writes the log entry.
        /// </summary>
        /// <param name="entry">Log entry.</param>
        public void WriteLogEntry(LogEntry entry)
        {
            if (entry == null)
                return;

            var eventType = EventLogEntryType.Information;

            // create category
            try
            {
                if (!EventLog.SourceExists(entry.Source))
                    EventLog.CreateEventSource(entry.Source, EventLogName);
            }
            catch
            {
                entry.Source = DiagnosticsResourceHelper.DefaultEventSource;
            }

            // get event type
            var enumAsString = entry.Type.ToString();
            if (Enum.IsDefined(typeof(EventLogEntryType), enumAsString))
                eventType = (EventLogEntryType)Enum.Parse(typeof(EventLogEntryType), enumAsString);

            // log the entry
            var log = new EventLog();
            log.Source = entry.Source;
            log.Log = EventLogName;
            log.WriteEntry(string.Concat(entry.Message, Environment.NewLine, entry.StackTrace), eventType);
        }
Beispiel #2
0
        /// <summary>
        /// Logs the specified message to EventLog.
        /// </summary>
        /// <param name="logEntry">The log entry.</param>
        public void Log(LogEntry logEntry)
        {
            try
            {
                // fill the missing data
                logEntry.EventTime = DateTime.Now;
                if (SPContext.Current != null)
                {
                    logEntry.HostName = SPContext.Current.Site.HostName;
                    logEntry.CurrentUserLogin = SPContext.Current.Web.CurrentUser.LoginName;
                }
                else
                    logEntry.HostName = Environment.MachineName;

                // write event to log for every configured target
                if (logEntry.Type != LogType.Internal)
                {
                    foreach (var target in _targets)
                        target.WriteLogEntry(logEntry.Clone());
                }
                else
                {
                    // if event is an "internal" type - log it only with system event logger
                    var eventLogger = _targets.Where(target => target.GetType() == typeof(EventLoggerTarget)).FirstOrDefault();

                    if (eventLogger != null)
                        eventLogger.WriteLogEntry(logEntry.Clone());
                }
            }
            catch
            {
                return;
            }
        }
 /// <summary>
 /// Writes the log entry.
 /// </summary>
 /// <param name="entry">Log entry.</param>
 public void WriteLogEntry(LogEntry entry)
 {
     // if everything is set - start the log writing method in a separate thread
     if (entry != null && !string.IsNullOrEmpty(ListName) && !string.IsNullOrEmpty(SiteUrl))
         ThreadPool.QueueUserWorkItem(new WaitCallback(WriteLogEntryAsync), entry);
 }
Beispiel #4
0
        /// <summary>
        /// Logs the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="logType">Type of the log.</param>
        public void Log(string message, LogType logType)
        {
            var logEntry = new LogEntry
            {
                Message = message,
                Type = logType
            };

            Log(logEntry);
        }
Beispiel #5
0
        /// <summary>
        /// Logs the specified exception.
        /// </summary>
        /// <param name="exception">The exception.</param>
        /// <param name="logType">Type of the log.</param>
        public void Log(Exception exception, LogType logType = LogType.Error)
        {
            // create log entry
            var logEntry = new LogEntry(exception);
            logEntry.Type = logType;

            Log(logEntry);
        }
Beispiel #6
0
        /// <summary>
        /// Logs the specified messageto EventLog.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="source">The source type.</param>
        /// <param name="logType">Type of the log.</param>
        public void Log(string message, SourceType source, LogType logType = LogType.Information)
        {
            var logEntry = new LogEntry
            {
                Message = message,
                Source = source.ToString(),
                Type = logType
            };

            Log(logEntry);
        }
Beispiel #7
0
        /// <summary>
        /// Logs the specified message to EventLog.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="source">The source.</param>
        /// <param name="logType">Type of the log.</param>
        public void Log(string message, string source, LogType logType = LogType.Information)
        {
            // create the log entry object
            var logEntry = new LogEntry
            {
                Message = message,
                Source = source ?? DiagnosticsResourceHelper.DefaultEventSource,
                Type = logType
            };

            Log(logEntry);
        }