[英]Log file is not being created by class library after installing the dll
我創建了一個具有某些功能的類庫,並維護了一個日志文件來跟蹤執行的步驟。 如果我在調試模式下運行此類庫,則說明它已成功創建日志文件。
但是,我已經在創建該類庫的tlb文件並將其安裝到系統中之后創建了cab文件。 現在我正在使用該庫,所有功能均正常運行,僅未寫入日志文件。
我使用以下代碼創建日志文件-
public static void LogTrace(string LogMessage)
{
try
{
string LogError = string.Empty;
String DirPath = string.Empty;
DirPath= Convert.ToString(ConfigurationSettings.AppSettings["DirPath"]);
LogError = Convert.ToString(ConfigurationSettings.AppSettings["LogError"]);
//if logging is not required
if (LogError.ToLower() == "true")
{
if (DirPath == null || DirPath == string.Empty)
DirPath = @"C:\LogAndError";
if (LogError == null || LogError == string.Empty)
LogError = "True";
//creation of date wise file name
string LogFileName = DirPath + "\\Log_ " + DateTime.Now.ToString("MM_dd_yyyy") + ".txt";
createLogAndErrorFile(DirPath);
StreamWriter streamWriter = null;
streamWriter = new StreamWriter(LogFileName, true);
streamWriter.WriteLine("Time :" + DateTime.Now.ToString() + " ");
streamWriter.WriteLine(LogMessage);
streamWriter.WriteLine(Environment.NewLine);
streamWriter.Flush();
streamWriter.Close();
}
}
catch
{
//We are not throwing any exception because all the exeption is logged using this method
//and throwing the exception could lead to recursive call of function.
}
}
public static void createLogAndErrorFile(string DirPath)
{
if (!Directory.Exists(DirPath))
Directory.CreateDirectory(DirPath);
}
}
我這么想嗎?
驗證您是否有權訪問文件
將您的代碼放在try catch塊中並捕獲異常,我建議您使用using塊
try
{
var path = Path.Combine(DirPath, string.Concat("\\Log_",DateTime.Now.ToString("MM_dd_yyyy"), ".txt"));
using (StreamWriter streamWriter = new StreamWriter(path))
{
streamWriter.WriteLine("Time :" + DateTime.Now.ToString() + " ");
streamWriter.WriteLine(LogMessage);
streamWriter.WriteLine(Environment.NewLine);
}
}
catch(Exception ex)
{
Console.Write(ex.Message);
throw ex;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.