C# 安装dll后,类库未创建日志文件

C# 安装dll后,类库未创建日志文件,c#,C#,我创建了一个类库,其中包含一些功能,并维护了一个日志文件来跟踪执行的步骤。如果我在调试模式下运行这个类库,它将成功地创建日志文件 但是,在创建该类库的tlb文件并安装到系统中之后,我创建了cab文件。现在我正在使用这个库,所有的函数都正常工作,只有日志文件没有被写入 我已经使用下面的代码创建了日志文件- public static void LogTrace(string LogMessage) { try { string L

我创建了一个类库,其中包含一些功能,并维护了一个日志文件来跟踪执行的步骤。如果我在调试模式下运行这个类库,它将成功地创建日志文件

但是,在创建该类库的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块并捕获异常 我建议你使用block

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

我没有问题,你的路径指向哪里?您在该目录中有写权限吗?如果(LogError.ToLower()=“true”)返回true,是否返回true?@CooLMinE我在DirPath中定义了“D:\LogsAndErrors”。。尽管它在构建模式下运行时正在写入日志。但是如果我只在另一个项目中使用dll,那么它不会写入..请确保在客户端配置中定义了
LogError
,或者更改第一个`if(LogError==“true”)。我还建议您不要吞咽错误,而是让它抛出未处理的异常或退回到事件日志。没有证据表明您试图调试此问题。首先从那里开始。@Gary.S在配置文件中的LogError==“true”。。我真的告诉过你它在工作。但是当我在另一个项目中使用dll时,它不工作,我已经告诉过它是正确的。但是当在其他应用程序中使用dll时,它并没有写入日志。否。。如果发生任何异常,我无法在dll中知道。