Logging App Block Programmatically?

Topics: Logging Application Block
Jul 27, 2009 at 5:48 PM

Hi,

I want to know, can i use logging block programmatically? without the config? can i code everything? please provide code snippet.

Glen

Jul 28, 2009 at 5:45 AM

You can, but it's not worth it. Basically, you need to write code that fills in the LoggingSettings object, and all the corresponding objects contained therein, and hope you got all the names lined up right. It's huge, it's fragile, and it's pretty much impossible to get right. If you really want an example, look through out unit tests for things setting up a DictionaryConfigurationSource.

We're going to be improving this vastly in Entlib 5; look for our new programmatic config interface in our next drop.

Jul 28, 2009 at 6:04 AM

Hi,

Here is a tutorial by David Hayden to use the LAB programmatically : http://davidhayden.com/blog/dave/archive/2006/02/18/2805.aspx

Below is the sample code snippet that I've come with trying the tutorial.

static void Main(string[] args)
        {
            LogWriter _writer;

            TextFormatter formatter = new TextFormatter
            ("Timestamp: {timestamp}{newline}" +
            "Message: {message}{newline}" +
            "Category: {category}{newline}");

            //Create the Trace listeners
            FlatFileTraceListener logFileListener = new FlatFileTraceListener(@"C:\myLog.log", "====HEADER====",
                    "====FOOTER====", formatter);
            FormattedEventLogTraceListener eventLogListener = new FormattedEventLogTraceListener("Enterprise Library Logging", formatter);


            //Add the trace listeners to the source
            LogSource mainLogSource =
            new LogSource("MainLogSource", SourceLevels.All);
                mainLogSource.Listeners.Add(logFileListener);
                mainLogSource.Listeners.Add(eventLogListener);

            LogSource nonExistantLogSource = new LogSource("Empty");

            IDictionary<string, LogSource> traceSources = 
                new Dictionary<string, LogSource>();
                traceSources.Add("Error", mainLogSource);
                traceSources.Add("Debug", mainLogSource);


                _writer = new LogWriter(new ILogFilter[0],
                            traceSources,
                            nonExistantLogSource,
                            nonExistantLogSource,
                            mainLogSource,
                            "Error",
                            false,
                            true);

            LogEntry entry = new LogEntry();
            entry.Message = "Done Programmatically.";
            entry.Categories.Add("Debug");
            _writer.Write(entry);
        }

 

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jul 30, 2009 at 2:11 AM

Thanks for the reply. I was trying to create a database trace listener and I'm having a hard time in that. Can you show me how to create it?

Jul 30, 2009 at 6:11 AM

Hi,

Here is the updated code snippet showing how to create the DatabaseTraceListener programmatically.

static void Main(string[] args)
        {
            LogWriter _writer;

            TextFormatter formatter = new TextFormatter
            ("Timestamp: {timestamp}{newline}" +
            "Message: {message}{newline}" +
            "Category: {category}{newline}");

            //Create the Trace listeners
            FlatFileTraceListener logFileListener = new FlatFileTraceListener(@"C:\myLog.log", "====HEADER====",
                    "====FOOTER====", formatter);
            FormattedEventLogTraceListener eventLogListener = new FormattedEventLogTraceListener("Enterprise Library Logging", formatter);

            //Addded the Database trace listener, constructed programmatically
            string connectionString = "Data Source=.;Initial Catalog=Logging;Integrated Security=true;";
            SqlDatabase db = new SqlDatabase(connectionString);

            //Passing the stored procedure that was created by running the LoggingDatabase.sql
            FormattedDatabaseTraceListener databaseListener = new FormattedDatabaseTraceListener(db,
                "WriteLog", "AddCategory", formatter);

            //Add the trace listeners to the source
            LogSource mainLogSource =
            new LogSource("MainLogSource", SourceLevels.All);
            mainLogSource.Listeners.Add(logFileListener);
            mainLogSource.Listeners.Add(eventLogListener);
            //add the database trace listener to the trace listeners
            mainLogSource.Listeners.Add(databaseListener);

            LogSource nonExistantLogSource = new LogSource("Empty");

            IDictionary<string, LogSource> traceSources = 
                new Dictionary<string, LogSource>();
                traceSources.Add("Error", mainLogSource);
                traceSources.Add("Debug", mainLogSource);


                _writer = new LogWriter(new ILogFilter[0],
                            traceSources,
                            nonExistantLogSource,
                            nonExistantLogSource,
                            mainLogSource,
                            "Error",
                            false,
                            true);

            LogEntry entry = new LogEntry();
            entry.Message = "Done Programmatically.";
            entry.Categories.Add("Debug");
            _writer.Write(entry);
        }

 

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com