简体   繁体   English

日志文件没有被类库安装DLL后创建

[英]Log file is not being created by class library after installing the dll

I have created a class library with some functionality and maintaining a log file to track the steps executed. 我创建了一个具有某些功能的类库,并维护了一个日志文件来跟踪执行的步骤。 If i run this class library in debug mode it is successfully creating log file. 如果我在调试模式下运行此类库,则说明它已成功创建日志文件。

But, I have created the cab file after creating the tlb file of that class library and installed into the system. 但是,我已经在创建该类库的tlb文件并将其安装到系统中之后创建了cab文件。 Now i am using that library, all the functions are working properly, only logs files are not written. 现在我正在使用该库,所有功能均正常运行,仅未写入日志文件。

I've used the code to create log file is below- 我使用以下代码创建日志文件-

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

Am i missing something ? 我这么想吗?

Verify that you have permission to acces to your file 验证您是否有权访问文件

Place your code in try catch block and capture exception I suggest you to use using block 将您的代码放在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM