/// <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;
        }
Example #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.");
     }
 }
Example #3
0
        private void logExtraInformationButton_Click(object sender, System.EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                this.DisplayScenarioStart(Properties.Resources.ExtraInformationStartMessage);

                // Use the WindowsPrincipal as the current principal. This will cause the
                // ManagedSecurityContextInformationProvider to add the current Windows user's name
                // to the additional information to be logged.
                AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

                // Create the dictionary to hold the extra information, and populate it
                // with managed security information.
                Dictionary<string, object> dictionary = new Dictionary<string, object>();
                ManagedSecurityContextInformationProvider informationHelper = new ManagedSecurityContextInformationProvider();

                informationHelper.PopulateDictionary(dictionary);

                // Add a custom property for screen resolution
                int width = Screen.PrimaryScreen.Bounds.Width;
                int height = Screen.PrimaryScreen.Bounds.Height;
                string resolution = String.Format("{0}x{1}", width, height);

                dictionary.Add("Screen resolution", resolution);

                // Write the log entry that contains the extra information
                Logger.Write("Log entry with extra information", dictionary);

                this.DisplayResults(Properties.Resources.ExtraInformationEndMessage);
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
        /// <summary>
        /// It checks Filter Status Before constructing Log Messages and
        /// writes the specified category by populating a Log Message with 
        /// Additional Context Information.
        /// </summary>
        /// <param name="priority">The priority.</param>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="categories">The categories.</param>
        public static void Write(TraceEventType eventType, string message, Dictionary<string, object> dictionary,
            params string[] categories)
        {
            var logEntry = new LogEntry {Severity = eventType, Message = message, Categories = categories};

            // Event will be logged according to currently configured filters.
            // Perform operations (possibly expensive) to gather additional information
            // for the event to be logged. Otherwise, event will not be logged.
            //if (Logger.GetFilter<CategoryFilter>().ShouldLog(categories))
            //{
                var informationHelper =
                    new ManagedSecurityContextInformationProvider();
                informationHelper.PopulateDictionary(dictionary);
                logEntry.ExtendedProperties = dictionary;
                Logger.Write(logEntry);
            //}
        }