Question

I am using Enterprise Library 5.0 in my win-form Application.

1. Regarding creating instances of Enterprise Library objects

What is the best way to Resolve the reference for Logging / exception objects? In our application, we have different applications in solution. So Solutions have below project:

CommonLib (Class Lib) CustomerApp (winform app) CustWinService (win service proj) ClassLib2 (class Lib)

I have implemented logging / exceptions as below in CommonLib project. Created a class AppLog as below:

 public class AppLog
    {
        public static LogWriter defaultWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
public static ExceptionManager exManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
        public AppLog()
        {
        }

    public static void WriteLog(string LogMessage, string LogCategories)
    {
        // Create a LogEntry and populate the individual properties.
            if (defaultWriter.IsLoggingEnabled())
            {
                string[] Logcat = LogCategories.Split(",".ToCharArray());
                LogEntry entry2 = new LogEntry();
                entry2.Categories = Logcat;
                entry2.EventId = 9007;
                entry2.Message = LogMessage;
                entry2.Priority = 9;
                entry2.Title = "Logging Block Examples";
                defaultWriter.Write(entry2);
            }
    }
}

And then I used Applog class as below for logging and exception in different projects:

            try
            {
                AppLog.WriteLog("This is Production Log Entry.", "ExceCategory");
                string strtest = string.Empty;
                strtest = strtest.Substring(1);
            }
            catch (Exception ex)
            {

                bool rethrow = AppLog.exManager.HandleException(ex, "ExcePolicy");

            }

So its the correct way to use Logging and Exception? or any other way i can improve it?

2. Logging File Name dynamic

In logging block, we have fileName which need to be set in app.config file. Is there a way I can assign fileName value dynamically through coding? Since I don't want to hard code it in config file and paths are different for production and development environment.

Thanks TShah

Was it helpful?

Solution

To keep your application loosely coupled and easier to test, I would recommend defining separate logging and exception handling interfaces, then having your AppLog class implement both. Your application can then perform logging and exception handling via those interfaces, with AppLog providing the implementation.

You can have a different file name set per environment using config transforms, which I believe you can use in a winforms application by using Slow Cheetah.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top