DCSIMG
Using Logging Application Block in Azure - Manu Cohen-Yashar's Blog

Manu Cohen-Yashar's Blog

Using Logging Application Block in Azure

Enterprise Library provides many important applications blocks that can be used to simply the implementation of infrastructure most applications need. One of the most popular application block is the logging application block.

The logging application block implements a simple to use yet powerful logger.
You can plug-in listeners that will store the logs in many different locations such as file , Event log, and a database.

many applications use the flat file listener and leverage its simplicity. When migrating to Windows Azure and using a large number instances we would like to continue to use Enterprise Library and enjoy Azure Diagnostics aggregation.

It is possible to configure Windows Azure Diagnostics to collect all the files located at a specified path. In Windows Azure we cannot just write to the file system. The local storage is a location on the local disk we can use. So let us configure that everything that will be written to the local storage will be collected.

DiagnosticMonitorConfiguration diagConfig = roleInstanceDiagnosticManager.GetCurrentConfiguration();
 
LocalResource localResource = RoleEnvironment.GetLocalResource("MyCustomLogs");
DirectoryConfiguration dirConfig = new DirectoryConfiguration();
dirConfig.Container = "wad-my-custom-container";
dirConfig.DirectoryQuotaInMB = localResource.MaximumSizeInMegabytes;
dirConfig.Path = localResource.RootPath;
diagConfig.Directories.DataSources.Add(dirConfig);
 
roleInstanceDiagnosticManager.SetCurrentConfiguration(diagConfig);

Now it is time to tell Enterprise Library to set the path of the flat file listener to the LocalStorage path.

The problem is that most applications use configuration files to set Enterprise Library logging but the path of the local storage is known only at runtime.

Fortunately it is possible to configure the Enterprise Library logger programmatically. The following code shows how to use Enterprise Library logging without configuration files at all. This way you can set the path in code :
_logger = new EnterpriseLibLogger(Path.Combine(RoleEnvironment.GetLocalResource("MyCustomLogs").RootPath, "log.txt"));

public class EnterpriseLibLogger  {    static LogWriter _writer;    public EnterpriseLibLogger(string path)    {        if (_writer == null)                     _writer = CreateWriter(path);                }    private LogWriter CreateWriter(string path)    {        string GeneralCategory = System.Diagnostics.EventLogEntryType.Information.ToString();        string ErrorCategory = System.Diagnostics.EventLogEntryType.Error.ToString();        var formatter = new TextFormatter(            "Timestamp: {timestamp}{newline}" +            "Category: {category}{newline}" +            "Message: {message}{newline}" +             "Extended Properties: {dictionary({key} - {value}{newline})}");        var logListener = new FlatFileTraceListener(path, string.Empty, string.Empty, formatter);        //this source has our listener        var errLogSource = new LogSource(ErrorCategory, SourceLevels.All);        errLogSource.Listeners.Add(logListener);                   // Don't log to this source        var emptyLogSource = new LogSource("Empty");        // "Error" category goes to main log source        var traceSources = new Dictionary<string, LogSource>         {             { ErrorCategory, errLogSource }                         };        // filter "Error" category        var categoryFilter = new CategoryFilter("Errors and Info", 
new List<string> { ErrorCategory, GeneralCategory },
CategoryFilterMode.DenyAllExceptAllowed);        var filters = new List<ILogFilter> { categoryFilter };        // in EntLib5 can't use LogWriter (it's abstract) or LogWriterFactory (which uses IServiceLocator)        return new LogWriterImpl(        //The collection of filters to use when processing an entry        filters,        //The trace sources to dispatch entries to        traceSources,        //The special LogSource to which all log entries should be logged.        errLogSource,        //The special LogSource to which log entries with at least one non-matching category should be logged        emptyLogSource,        //The special LogSource to which internal errors must be logged        emptyLogSource,        //The default category to set when entry categories list of a log entry is empty        GeneralCategory,        //The tracing status        false,        //true if warnings should be logged when a non-matching category is found        false);    }             public override void LogInfo(string info, System.Diagnostics.EventLogEntryType entryType)    {       _writer.Write(info, entryType.ToString());                            }          }

 

Enjoy

Manu

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: