Overload of excellence

I have two static methods that I want to use for error handling. One of them passes an exception object, and the other is used only if it needs to report an error, which will be a text message (errorMessage line).

The code in the two methods is almost the same, except for how the message is created and sent to the log file. How can I reorganize this so as not to duplicate the code?

public static void ReportError(Exception exceptionRaised, string reference, string customMessage, bool sendEmail)
{
    // get filename
    // check if logfile exists, blah, blah
    // build up message from exception, reference & custom message using string builder
    // save message
    // email error (if set)
}

public static void ReportError(string errorMessage, string reference, bool sendEmail)
{
    // get filename
    // check if logfile exists, blah, blah
    // build up message from errorMessage & reference string builder
    // save message
    // email error (if set)
}

Thank.

+3
source share
6 answers

One simple solution would be to extract the duplicate code in your own method, for example:

public static void ReportError(Exception exceptionRaised, string reference, string customMessage, bool sendEmail)
{
    // build up message from exception, reference & custom message using string builder
    ProcessError(Message, SendEmail)
}

public static void ReportError(string errorMessage, string reference, bool sendEmail)
{
    // build up message from errorMessage & reference string builder
    ProcessError(Message, SendEmail)
}

private static void ProcessError(string message, bool sendEmail)
{
    // get filename
    // check if logfile exists, blah, blah
    // save message
    // email error (if set)
}

This, of course, is not the most elegant way to do this, but it is a beginning; -)

+3
source

, , -, , , :

public static void ReportError(Exception exceptionRaised, string reference, 
    string customMessage, bool sendEmail)
{
    string errorMessage = BuildMessage(exceptionRaised, customMessage);
    ReportError(errorMessage, reference, sendEmail);
}

: , . , .

EDIT:

:

private static void ReportError(string completeException, bool sendEmail)
{
     // Do what needs to be done.
}

sendEmail boolean .

+9

, , null "" ?

public static void ReportError(string errorMessage,
                               string reference,
                               bool sendEmail)
{
    ReportError(errorMessage, reference, null, sendEmail);
}

, # 4, , .

+5

, . . .

public static void ReportError(string customMessage, string reference, bool sendEmail, Exception exceptionRaised) 

. , . .

+1
void report(Exception e)
{
  //convert exception to plain text
  string text = e.ToString();
  //re-use the other overload
  report(text);
}

void report(string text)
{
  ...etc...
}

:

delegate string GetString();

public void report(Exception e)
{
  GetString getString = delegate() { return e.ToString(); }
  report(getText);
}

public void report(string text)
{
  GetString getString = delegate() { return text; }
  report(getText);
}

void report(GetString getString)
{
  ...etc...
  string text = getString();
  ...etc...
}
0

, , -, , , , ...

public void report(string text)
{
   ...
   report(text,defaultvalue);
}

public void report(string text, string email)
{
   ...
   report(text,email,null);
}

public void report(string text, string email, List<string> errors)
{
   ...
}
...
etc.

, , , , .

0

Source: https://habr.com/ru/post/1763041/


All Articles