【问题标题】:Enterprise Library 5: Creating instances of Enterprise Library objects企业库 5:创建企业库对象的实例
【发布时间】:2012-12-04 15:36:59
【问题描述】:

我在我的 win-form 应用程序中使用 Enterprise Library 5.0。

1.关于创建企业库对象的实例

解决日志记录/异常对象引用的最佳方法是什么?在我们的应用程序中,我们在解决方案中有不同的应用程序。所以解决方案有以下项目:

CommonLib(类库) 客户应用程序(winform 应用程序) CustWinService(赢服务项目) ClassLib2(类库)

我在 CommonLib 项目中实现了如下的日志记录/异常。创建了一个 AppLog 类如下:

 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);
            }
    }
}

然后我使用如下 Applog 类在不同项目中进行日志记录和异常:

            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");

            }

那么它是使用 Logging 和 Exception 的正确方法吗?或者有什么其他方法可以改进吗?

2。动态记录文件名

在日志记录块中,我们有需要在 app.config 文件中设置的文件名。有没有办法可以通过编码动态分配文件名值?由于我不想在配置文件中对其进行硬编码,并且生产和开发环境的路径不同。

谢谢 沙阿

【问题讨论】:

    标签: winforms enterprise-library-5


    【解决方案1】:

    为了让您的应用程序保持松散耦合并更易于测试,我建议您定义单独的日志记录和异常处理接口,然后让您的 AppLog 类实现这两者。然后,您的应用程序可以通过这些接口执行日志记录和异常处理,AppLog 提供实现。

    您可以使用配置转换为每个环境设置不同的文件名,我相信您可以通过 Slow Cheetah 在 winforms 应用程序中使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-17
      相关资源
      最近更新 更多