/// <summary> /// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code. /// Builds a <see cref="FormattedEventLogTraceListener"/> based on an instance of <see cref="FormattedEventLogTraceListenerData"/>. /// </summary> /// <seealso cref="TraceListenerCustomFactory"/> /// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param> /// <param name="objectConfiguration">The configuration object that describes the object to build. Must be an instance of <see cref="FormattedEventLogTraceListenerData"/>.</param> /// <param name="configurationSource">The source for configuration objects.</param> /// <param name="reflectionCache">The cache to use retrieving reflection information.</param> /// <returns>A fully initialized instance of <see cref="FormattedEventLogTraceListener"/>.</returns> public override TraceListener Assemble(IBuilderContext context, TraceListenerData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) { FormattedEventLogTraceListenerData castedObjectConfiguration = (FormattedEventLogTraceListenerData)objectConfiguration; ILogFormatter formatter = GetFormatter(context, castedObjectConfiguration.Formatter, configurationSource, reflectionCache); TraceListener createdObject = new FormattedEventLogTraceListener( castedObjectConfiguration.Source, castedObjectConfiguration.Log, castedObjectConfiguration.MachineName, formatter); return createdObject; }
protected void ShoutLoop() { // Configue a LogWriter to write to the Windows Event Log. var eventLog = new EventLog("Application", ".", "ShoutService"); var eventLogTraceListener = new FormattedEventLogTraceListener(eventLog) { Filter = new SeverityFilter(new TraceEventType[] { TraceEventType.Error, TraceEventType.Information, TraceEventType.Warning}) }; var config = new LoggingConfiguration(); config.AddLogSource("All", SourceLevels.All, true) .AddTraceListener(eventLogTraceListener); // A using statement ensures the log gets flushed when this process exits. using (var logWriter = new LogWriter(config)) { var shouter = new Shouter(logWriter); do { shouter.Shout(cancellationTokenSource.Token); } while (!cancellationTokenSource.Token.IsCancellationRequested); } }
private LoggingConfiguration BuildProgrammaticConfig() { // Formatters TextFormatter briefFormatter = new TextFormatter("Timestamp: {timestamp(local)}{newline}Message: {message}{newline}Category: {category}{newline}Priority: {priority}{newline}EventId: {eventid}{newline}ActivityId: {property(ActivityId)}{newline}Severity: {severity}{newline}Title:{title}{newline}"); TextFormatter extendedFormatter = new TextFormatter("Timestamp: {timestamp}{newline}Message: {message}{newline}Category: {category}{newline}Priority: {priority}{newline}EventId: {eventid}{newline}Severity: {severity}{newline}Title: {title}{newline}Activity ID: {property(ActivityId)}{newline}Machine: {localMachine}{newline}App Domain: {localAppDomain}{newline}ProcessId: {localProcessId}{newline}Process Name: {localProcessName}{newline}Thread Name: {threadName}{newline}Win32 ThreadId:{win32ThreadId}{newline}Extended Properties: {dictionary({key} - {value}{newline})}"); // Category Filters ICollection<string> categories = new List<string>(); categories.Add("BlockedByFilter"); // Log Filters var priorityFilter = new PriorityFilter("Priority Filter", 2, 99); var logEnabledFilter = new LogEnabledFilter("LogEnabled Filter", true); var categoryFilter = new CategoryFilter("Category Filter", categories, CategoryFilterMode.AllowAllExceptDenied); // Trace Listeners //var causeLoggingErrorTraceListener = new FormattedDatabaseTraceListener(DatabaseFactory.CreateDatabase("DoesNotExist"), "WriteLog", "AddCategory", null); //var databaseTraceListener = new FormattedDatabaseTraceListener(DatabaseFactory.CreateDatabase("ExampleDatabase"), "WriteLog", "AddCategory", extendedFormatter); var flatFileTraceListener = new FlatFileTraceListener(@"C:\Temp\FlatFile.log", "----------------------------------------", "----------------------------------------", briefFormatter); var eventLog = new EventLog("Application", ".", "Enterprise Library Logging"); var eventLogTraceListener = new FormattedEventLogTraceListener(eventLog); var rollingFlatFileTraceListener = new RollingFlatFileTraceListener(@"C:\Temp\RollingFlatFile.log", "----------------------------------------", "----------------------------------------", extendedFormatter, 20, "yyyy-MM-dd", RollFileExistsBehavior.Increment, RollInterval.None, 3); var unprocessedFlatFileTraceListener = new FlatFileTraceListener(@"C:\Temp\Unprocessed.log", "----------------------------------------", "----------------------------------------", extendedFormatter); var xmlTraceListener = new XmlTraceListener(@"C:\Temp\XmlLogFile.xml"); xmlTraceListener.Filter = new EventTypeFilter(SourceLevels.Error); // Build Configuration var config = new LoggingConfiguration(); config.Filters.Add(priorityFilter); config.Filters.Add(logEnabledFilter); config.Filters.Add(categoryFilter); config.AddLogSource("BlockedByFilter", SourceLevels.All, true).AddTraceListener(eventLogTraceListener); //config.AddLogSource("CauseLoggingError", SourceLevels.All, true).AddTraceListener(causeLoggingErrorTraceListener); //config.AddLogSource("Database", SourceLevels.All, true).AddTraceListener(databaseTraceListener); // The defaults for the asynchronous wrapper are: // bufferSize: 30000 // disposeTimeout: infinite //config.AddLogSource("AsyncDatabase", SourceLevels.All, true).AddAsynchronousTraceListener(databaseTraceListener); config.AddLogSource("DiskFiles", SourceLevels.All, true).AddTraceListener(flatFileTraceListener); config.AddLogSource("General", SourceLevels.All, true).AddTraceListener(eventLogTraceListener); config.AddLogSource("Important", SourceLevels.All, true).AddTraceListener(eventLogTraceListener); config.LogSources["Important"].AddTraceListener(rollingFlatFileTraceListener); // Special Sources Configuration config.SpecialSources.Unprocessed.AddTraceListener(unprocessedFlatFileTraceListener); config.SpecialSources.LoggingErrorsAndWarnings.AddTraceListener(eventLogTraceListener); return config; }
public void ShouldThrowArgumentNull() { string name = null; var listener = new FormattedEventLogTraceListener(name); }
private static LoggingConfiguration BuildLoggingConfig() { // Formatters TextFormatter formatter = new TextFormatter("Timestamp: {timestamp}{newline}Message: {message}{newline}Category: {category}{newline}Priority: {priority}{newline}EventId: {eventid}{newline}Severity: {severity}{newline}Title:{title}{newline}Machine: {localMachine}{newline}App Domain: {localAppDomain}{newline}ProcessId: {localProcessId}{newline}Process Name: {localProcessName}{newline}Thread Name: {threadName}{newline}Win32 ThreadId:{win32ThreadId}{newline}Extended Properties: {dictionary({key} - {value}{newline})}"); // Listeners var flatFileTraceListener = new FlatFileTraceListener(@"C:\Temp\SalaryCalculator.log", "----------------------------------------", "----------------------------------------", formatter); var eventLog = new EventLog("Application", ".", "Enterprise Library Logging"); var eventLogTraceListener = new FormattedEventLogTraceListener(eventLog); // Build Configuration var config = new LoggingConfiguration(); config.AddLogSource("General", SourceLevels.All, true).AddTraceListener(eventLogTraceListener); config.LogSources["General"].AddTraceListener(flatFileTraceListener); // Special Sources Configuration config.SpecialSources.LoggingErrorsAndWarnings.AddTraceListener(eventLogTraceListener); return config; }