private Logger()
		{
			string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
			TextFormatter formatter = new TextFormatter();
			var databaseTraceListener =
			  new FormattedDatabaseTraceListener(new SqlDatabase(connectionString)
			  , "WriteLog", "AddCategory", formatter);

			var config = new LoggingConfiguration();
			config.AddLogSource(LogCategories.General.ToString(), SourceLevels.All, true).AddTraceListener(databaseTraceListener);
			writer = new LogWriter(config);
			writer.Write("Starting...", LogCategories.General.ToString(), 9, 0, TraceEventType.Verbose, "Log Starting");
		}
Beispiel #2
0
        static 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.LogSources["DiskFiles"].AddTraceListener(xmlTraceListener);
          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;
        }