Skip to main content
added 80 characters in body
Source Link
Greg D
  • 44.2k
  • 13
  • 88
  • 118

Have you considered factoring the // write ex.message to another log file behavior into a separate function and calling that with your desired string?

    if(CheckForSomething())
    {
        try
        {
            // do something
        }
        catch (UnauthorizedAccessException ex)
        {
            LogException(ex);
        }
        catch (Exception ex) // Never do this.  Do you know how to handle an OutOfMemoryException?
        {
            LogException(ex);
        }
    }
    else
    {
        string error = "Some error";
        LogMessage(error);
    }
 

private static void LogException(Exception ex)
{
    // write ex to one log file
    LogMessage(ex.Message);
}

private static void LogMessage(string message)
{
    // write message to another log file
}

Have you considered factoring the // write ex.message to another log file behavior into a separate function and calling that with your desired string?

if(CheckForSomething())
{
    try
    {
        // do something
    }
    catch (UnauthorizedAccessException ex)
    {
        LogException(ex);
    }
    catch (Exception ex) // Never do this.  Do you know how to handle an OutOfMemoryException?
    {
        LogException(ex);
    }
}
else
{
    string error = "Some error";
    LogMessage(error);
}
 

private static void LogException(Exception ex)
{
    // write ex to one log file
    LogMessage(ex.Message);
}

private static void LogMessage(string message)
{
    // write message to another log file
}

Have you considered factoring the // write ex.message to another log file behavior into a separate function and calling that with your desired string?

    if(CheckForSomething())
    {
        try
        {
            // do something
        }
        catch (UnauthorizedAccessException ex)
        {
            LogException(ex);
        }
        catch (Exception ex) // Never do this.  Do you know how to handle an OutOfMemoryException?
        {
            LogException(ex);
        }
    }
    else
    {
        string error = "Some error";
        LogMessage(error);
    }

private static void LogException(Exception ex)
{
    // write ex to one log file
    LogMessage(ex.Message);
}

private static void LogMessage(string message)
{
    // write message to another log file
}
Source Link
Greg D
  • 44.2k
  • 13
  • 88
  • 118

Have you considered factoring the // write ex.message to another log file behavior into a separate function and calling that with your desired string?

if(CheckForSomething())
{
    try
    {
        // do something
    }
    catch (UnauthorizedAccessException ex)
    {
        LogException(ex);
    }
    catch (Exception ex) // Never do this.  Do you know how to handle an OutOfMemoryException?
    {
        LogException(ex);
    }
}
else
{
    string error = "Some error";
    LogMessage(error);
}


private static void LogException(Exception ex)
{
    // write ex to one log file
    LogMessage(ex.Message);
}

private static void LogMessage(string message)
{
    // write message to another log file
}