Click here to Skip to main content
14,326,100 members
Rate this:
Please Sign up or sign in to vote.
See more:
I've got a log file that's created, if it doesn't exist, the first time it is referenced:

if (!File.Exists(logPath))
{
    _fileStream = File.Create(logPath);
}


Yet if the file doesn't exist, the app will crash with the overly generic exception msg, "Application bla encountered a serious error and must shut down"

It then has the file in question (with nothing in it), so subsequent attempts to run the app go just fine. Why does not the JIT creating of the file cut this problem off at the proverbial pass? Here is the code in context (the entire logging class):

using System;

namespace HHS
{
    using System.IO;
    using System.Text;

    public class ExceptionLoggingService
    {
        // Singleton code
        // private fields
        private readonly FileStream _fileStream;
        private readonly StreamWriter _streamWriter;
        private static ExceptionLoggingService _instance;
        // public property
        public static ExceptionLoggingService Instance
        {
            get
            {
                return _instance ?? (_instance = new ExceptionLoggingService());
            }
        }
        //private constructor
        private ExceptionLoggingService()
        {
            const int MAX_FILESIZE_ALLOWED = 40000;
            string uriPath = GetExecutionFolder() + "\\Application.log";
            string logPath = new Uri(uriPath).LocalPath;
            // If the log file does not exist, the app fails with a cryptic err msg (even with this code!)
            if (!File.Exists(logPath))
            {
                _fileStream = File.Create(logPath);
            }
            FileInfo f = new FileInfo(logPath);
            long fileSizeInBytes = f.Length;
            if (fileSizeInBytes > MAX_FILESIZE_ALLOWED) 
            {
                File.Delete(logPath);
            }
            _fileStream = File.OpenWrite(logPath);
            _streamWriter = new StreamWriter(_fileStream);
        }
        // </ Singleton code

        ~ ExceptionLoggingService()
        {
            if (null != _fileStream)
            {
                _fileStream.Close();
                _fileStream.Dispose();
            }
            if (null != _streamWriter)
            {
                _streamWriter.Close();
                _streamWriter.Dispose();
            }
        }

        public void WriteLog(string message)
        {
            if (!HHSConsts.Logging) return;
            StringBuilder formattedMessage = new StringBuilder();
            formattedMessage.AppendLine("Date: " + DateTime.Now.ToString());
            formattedMessage.AppendLine("Message: " + message);
            _streamWriter.WriteLine(formattedMessage.ToString());
            _streamWriter.Flush();
        }

        private string GetExecutionFolder()
        {
            return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); 
        }

    }
}


Why does the file being missing cause a problem when that possibility is being handled?
Posted
Rate this:
Please Sign up or sign in to vote.

Solution 1

try to dispose the file stream when you create it first time

if (!File.Exists(logPath))
            {
                using (_fileStream = File.Create(logPath)){};
            }
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Don't you think that this is a very natural contradiction: with the logging mechanism, you can collect all the information which could reveal your bugs, but what if you make a bug in logging itself?

Apparently, the solution is: develop and well debug logging mechanism in advance, before you get to debugging something more complex using it. Such tools are already done for you.

One is the class System.Diagnostic.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog%28v=vs.110%29.aspx[^].

Another one is well known and widely used product Apache Log4Net:
http://en.wikipedia.org/wiki/Log4j#Ports[^],
http://logging.apache.org/log4net[^].

Maybe you can use something else…

—SA
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

if (!File.Exists(logPath))
{
	_fileStream = File.Create(logPath);
	_fileStream.Dispose();
	
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100