locked
Microsoft Enterprise Library Logging RRS feed

  • Question

  • User772244099 posted

    Hello All ,

    I am planning to use Microsoft Enterprise Logging for Error logging . My requirement is to

    1. Log the Errors in the log file and also in the Database.

    2. Create Event Log.

    Thanks

    Friday, January 16, 2015 4:01 AM

Answers

All replies

  • User-1034726716 posted

    Start looking these references to get started:

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 16, 2015 4:07 AM
  • User772244099 posted

    Based on the second link , there is a console that would generate the Configuration file . As I am using Visual Studio 2013 , I am not able to get the same . :(

    Monday, January 19, 2015 10:13 PM
  • User772244099 posted

    I would like to have the confiurations in the config file . How can i do that ..

    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;

    }

    Wednesday, January 21, 2015 9:12 PM