1

StackOverFlowExceprion raised, when logDirectory = new DirectoryInfo(m_logFileDirectory); line was executing. How can I solve the problem? Please help me. This procedure writeLogToFile write a logMessge to File to logDirectory. It is part of Log write program.

    private void writeLogToFile(string logMessge)
    {
        DirectoryInfo logDirectory = null;
    
        try
        {
            Monitor.Enter(m_lock);
    
            logDirectory = new DirectoryInfo(m_logFileDirectory);
            if (!logDirectory.Exists)
            {
               logDirectory.Create();
            }
    
            if (m_streamWriter == null) newLogFile();
    
            if (m_logFileAutoSeperate && (m_streamWriter.BaseStream.Length > m_logFileSize * 1024))
            {
                 newLogFile();
                 m_streamWriter.WriteLine(logMessge);
            }
            else
            {
                m_streamWriter.WriteLine(logMessge);
            }
    
            // stream writer uses  internal buffer.
            // if directWriting option is true, the file will be write(flush) once.
    
            if (directWriting) m_streamWriter.Flush();
       }
       catch (StackOverflowException sofex)
       {
           AppExceptionHandler.writeException(sofex);
       }
       catch (AppException aex)
       {
           AppExceptionHandler.writeException(aex);
       }
       catch (Exception ex)
       {
           AppExceptionHandler.writeException(ex);
       }
       finally
       {
           Monitor.Exit(m_lock);
       }
    }

From the comments, this is what newLogFile looks like:

private void newLogFile() 
{
    try 
    { 
        Monitor.Enter(m_lock); 
        if (m_streamWriter != null) 
        { 
            endLogging(); 
            m_streamWriter.Flush(); 
            m_streamWriter.Close();
        }
        m_logFileNameSuffix = getLogFileNewSuffix();
        m_streamWriter = File.AppendText(getLogFileName());
        startLogging(); 
    }
}
René Vogt
  • 43,056
  • 14
  • 77
  • 99
Sujin Lee
  • 11
  • 2
  • SOE isn't normally catchable (you're already toast when that happens), so: probably best not to try; it doesn't *look* like you're doing a stack-dive here, and I *doubt* that `new DirectoryInfo` is either; is it possible that you are genuinely near the edge of the stack, by coincidence, and that this is the victim not the perpetrator? (for context, [here](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/IO/DirectoryInfo.cs#L16) is the current BCL code; doesn't look recursive) (note: if all your catch handlers do the same thing: you only need the one) – Marc Gravell Oct 06 '22 at 07:17
  • Can you get a stack trace for the exception? – jmcilhinney Oct 06 '22 at 07:21
  • 3
    What does `newLogFile()` do? And did you debug? @jmcilhinney It's a StackOverflowException, so the stack is already corrupt, I'm not sure, but I don't think this kind of exception has a useful stacktrace. – René Vogt Oct 06 '22 at 07:23
  • @jmcilhinney SOE is notoriously hard to catch reliably; it *may* be possible on a good day, but I wouldn't pin much hope on it; personally, I'd try to get the stack trace manually at the start of the method, to see how deep we are already - `Environment.StackTrace` may be sufficient; and if *that attempt* explodes, we're probably already too deep – Marc Gravell Oct 06 '22 at 07:23
  • @RenéVogt I suspect you're right on the money here - perhaps (speculating) `newLogFile` calls `writeLogToFile($"log started: {time}");` (or whatever) *before* it assigns `m_streamWriter`, causing a boom – Marc Gravell Oct 06 '22 at 07:24
  • 1
    alternatively: maybe `AppExceptionHandler.writeException` calls back to `writeLogToFile`, so the moment there's a problem *with the logger*, everything explodes – Marc Gravell Oct 06 '22 at 07:27
  • Does this answer your question? [DirectoryInfo.GetFiles throws unhandled StackOverflowException](https://stackoverflow.com/questions/40080760/directoryinfo-getfiles-throws-unhandled-stackoverflowexception) – Ibrennan208 Oct 06 '22 at 07:37
  • Can you ensure your `m_logFileDirectory` is a legitimate one? If all else is correct, I would also think that `newLogFile()` is the culprit, or `AppExceptionHandler`, as @MarcGravell has said. – Ibrennan208 Oct 06 '22 at 07:41
  • @René Vogt private void newLogFile() { try { Monitor.Enter(m_lock); if (m_streamWriter != null) { endLogging(); m_streamWriter.Flush(); m_streamWriter.Close(); } m_logFileNameSuffix = getLogFileNewSuffix(); m_streamWriter = File.AppendText(getLogFileName()); startLogging(); } } – Sujin Lee Oct 10 '22 at 23:21
  • @RenéVogt newLogFile() likes above. LogFile's Suffix writing, appending Text of newLogFile... – Sujin Lee Oct 10 '22 at 23:24

0 Answers0