Beispiel #1
0
        /// <summary>
        ///     Writes the specified entry to the event log writers,
        /// </summary>
        /// <param name="level">The error level.</param>
        /// <param name="source">The message source.</param>
        /// <param name="message">The message.</param>
        private void WriteEntry(EventLogLevel level, string source, string message)
        {
            DiagnosticsRequestContextData context = DiagnosticsRequestContext.GetContext();

            string userName   = string.Empty;
            long   tenantId   = -1;
            string tenantName = string.Empty;

            if (context != null)
            {
                userName   = context.UserName;
                tenantId   = context.TenantId;
                tenantName = context.TenantName;
            }

            // Truncate messages longer than 10000 chars
            if (message != null && message.Length > MaximumMessageSize)
            {
                message = message.Substring(0, MaximumMessageSize);
            }

            // Create an event log entry and pass it the writers.
            var logEntry = new EventLogEntry(DateTime.UtcNow,
                                             Stopwatch.GetTimestamp(),
                                             level,
                                             Thread.CurrentThread.ManagedThreadId,
                                             source ?? string.Empty,
                                             message ?? string.Empty,
                                             tenantId,
                                             tenantName,
                                             userName)
            {
                Machine = _machineName,
                Process = _processName
            };


            OnWriteEvent(logEntry);

            foreach (IEventLogWriter logWriter in _eventLogWriters)
            {
                try
                {
                    logWriter.WriteEntry(logEntry);
                }
                catch (Exception ex)
                {
                    // Prevent errors from one writer affecting others.
                    Trace.TraceError($"An error occured writing a message to a log writer. Writer type {logWriter.GetType().Name}, error {ex}.");
                }
            }
        }
 /// <summary>
 /// Sets the context data within the logical thread.
 /// </summary>
 /// <param name="tenantId">The tenant id.</param>
 /// <param name="tenantName">Name of the tenant.</param>
 /// <param name="userName">Name of the user.</param>
 public static void SetContext(long tenantId, string tenantName, string userName)
 {
     // Set the default request context data in the logical context
     _contextData = new DiagnosticsRequestContextData(tenantId, tenantName, userName);
 }
 /// <summary>
 ///     Sets the context data within the logical thread.
 /// </summary>
 /// <param name="contextData">The context data.</param>
 /// <remarks></remarks>
 public static void SetContext(DiagnosticsRequestContextData contextData)
 {
     _contextData = contextData;
 }
 /// <summary>
 ///     Frees the request data from the logical thread.
 /// </summary>
 public static void FreeContext()
 {
     // Free the request context data from the logical context
     _contextData = null;
 }