在我的win应用程序中,我正在使用企业库5.0 .
1.关于创建企业库对象实例
解析日志记录/异常对象的引用的最佳方法是什么?在我们的应用中,我们在解决方案中有不同的应用。因此,解决方案有以下项目:
CommonLib (Lib类) CustomerApp (winform应用程序) CustWinService (win service proj) ClassLib2 (Lib类)
我在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");
}那么它是使用日志记录和异常的正确方式吗?或者其他我能改进的方法?
2.日志文件名动态
在日志记录块中,我们有需要在fileName文件中设置的app.config。有没有一种方法可以通过编码动态地分配fileName值?因为我不想硬编码它在配置文件和路径是不同的生产和开发环境。
谢谢TShah
发布于 2012-12-05 08:52:09
为了保持应用程序的松散耦合和易于测试,我建议定义单独的日志记录和异常处理接口,然后让您的AppLog类实现这两个接口。然后,应用程序可以通过这些接口执行日志记录和异常处理,由AppLog提供实现。
您可以使用配置转换为每个环境设置不同的文件名,我相信您可以使用慢豹在winforms应用程序中使用。
https://stackoverflow.com/questions/13706433
复制相似问题