Пример #1
0
        /// <summary>
        /// Creates a preconfigured instance of a LogEntry.
        /// </summary>
        /// <param name="severity">Severity of the log entry.</param>
        /// <param name="message">Error message</param>
        /// <param name="title">Title of the log entry</param>
        /// <param name="includeSecurityDetail">if set to <c>true</c> security information is included in the log.</param>
        /// <returns>Configured LogEntry instance</returns>
        public LogEntry CreateInstance(
            TraceEventType severity, string message, string title, bool includeSecurityDetail)
        {
            LogEntry le = new LogEntry();

            if (string.IsNullOrEmpty(title))
            {
                le.Title = "Untitled";
            }
            else
            {
                le.Title = title;
            }

            le.Message = message;
            le.Severity = severity;

            Dictionary<string, object> dictionary = new Dictionary<string, object>();

            if (includeSecurityDetail)
            {
                ManagedSecurityContextInformationProvider informationHelper = new ManagedSecurityContextInformationProvider();
                informationHelper.PopulateDictionary(dictionary);

                UnmanagedSecurityContextInformationProvider unmanagedHelper = new UnmanagedSecurityContextInformationProvider();
                unmanagedHelper.PopulateDictionary(dictionary);
            }

            le.ExtendedProperties = dictionary;

            return le;
        }
Пример #2
0
 static void ShowDetailsAndAddExtraInfo(LogEntry entry)
 {
     // Display information about the Trace Sources and Listeners for this LogEntry.
     IEnumerable<LogSource> sources = defaultWriter.GetMatchingTraceSources(entry);
     foreach (LogSource source in sources)
     {
         Console.WriteLine("Log Source name: '{0}'", source.Name);
         foreach (TraceListener listener in source.Listeners)
         {
             Console.WriteLine(" - Listener name: '{0}'", listener.Name);
         }
     }
     // Check if any filters will block this LogEntry.
     // This approach allows you to check for specific types of filter.
     // If there are no filters of the specified type configured, the GetFilter
     // method returns null, so check this before calling the ShouldLog method.
     CategoryFilter catFilter = defaultWriter.GetFilter<CategoryFilter>();
     if (null == catFilter || catFilter.ShouldLog(entry.Categories))
     {
         Console.WriteLine("Category Filter(s) will not block this LogEntry.");
     }
     else
     {
         Console.WriteLine("A Category Filter will block this LogEntry.");
     }
     PriorityFilter priFilter = defaultWriter.GetFilter<PriorityFilter>();
     if (null == priFilter || priFilter.ShouldLog(entry.Priority))
     {
         Console.WriteLine("Priority Filter(s) will not block this LogEntry.");
     }
     else
     {
         Console.WriteLine("A Priority Filter will block this LogEntry.");
     }
     // Alternatively, a simple approach can be used to check for any type of filter
     if (defaultWriter.ShouldLog(entry))
     {
         Console.WriteLine("This LogEntry will not be blocked due to configuration settings.");
         // Create the additional context information to add to the LogEntry. Checking that
         // the LogEntry will not be blocked first minimizes the performance impact.
         Dictionary<string, object> dict = new Dictionary<string, object>();
         // Use the information helper classes to get information about the environment and add it to the dictionary.
         DebugInformationProvider debugHelper = new DebugInformationProvider();
         debugHelper.PopulateDictionary(dict);
         Console.WriteLine("Added the current stack trace to the Log Entry.");
         ManagedSecurityContextInformationProvider infoHelper = new ManagedSecurityContextInformationProvider();
         infoHelper.PopulateDictionary(dict);
         Console.WriteLine("Added current identity name, authentication type, and status to the Log Entry.");
         UnmanagedSecurityContextInformationProvider secHelper = new UnmanagedSecurityContextInformationProvider();
         secHelper.PopulateDictionary(dict);
         Console.WriteLine("Added the current user name and process account name to the Log Entry.");
         ComPlusInformationProvider comHelper = new ComPlusInformationProvider();
         comHelper.PopulateDictionary(dict);
         Console.WriteLine("Added COM+ IDs and caller account information to the Log Entry.");
         // Get any other information you require and add it to the dictionary.
         string configInfo = File.ReadAllText(@"..\..\App.config");
         dict.Add("Config information", configInfo);
         Console.WriteLine("Added information about the configuration of the application to the Log Entry.");
         // Set the dictionary in the LogEntry and write it using the default LogWriter.
         entry.ExtendedProperties = dict;
         defaultWriter.Write(entry);
         Console.WriteLine("LogEntry written to configured trace listeners.");
         Console.WriteLine();
     }
     else
     {
         Console.WriteLine("This LogEntry will be blocked due to configuration settings.");
     }
 }