Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7762381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:23:15+00:00 2026-06-01T14:23:15+00:00

Please check out my following code… public enum LogType { Debug, Info, Warn, Error,

  • 0

Please check out my following code…

public enum LogType
{
    Debug,
    Info,
    Warn,
    Error,
    Fatal
}

private static readonly ILog log = 
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

public void LogError(LogType logtype, string message)
{
    XmlConfigurator.Configure();
    if (logtype == LogType.Debug)
        log.Debug(message);
    else if (logtype == LogType.Error)
        log.Error(message);
}

I do not like all the above if-else statements and believe that there is a cleaner way to write this. How could I refactor it? log class has different methods for Debug, Error etc. etc.

I would like to make a single call to a method have it automatically take care of it.

LogMyError(LogType.Debug, "I am just logging here");

How can I do such a thing? I prefer to stay away from switch statement. I am looking for a clean object oriented approach.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:23:17+00:00Added an answer on June 1, 2026 at 2:23 pm

    You can use a Dictionary<LogType,Action<string>> to hold what action to perform for each enumeration value, then just call the delegate.

    var logActions = new Dictionary<LogType,Action<string>>();
    logActions.Add(LogType.Debug, log.Debug);
    ...
    
    logActions[logtype](message);
    

    Update:

    This is all overkill if you do only have a small number of branches on your if statements. I would use this method for 5+ such ifs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please check out my following code String jsonString = writer.toString(); JSONObject jsonObj = new
Please check out the following func and its output void main() { Distance d1;
Please check this code out it compiles and runs absolutely fine.. The question is
Please check out the following query. The SQL isn't as bad as it looks.
Please check out http://rainsongmanchesters.net/news.htm and notice the black strip above the header image. Now
Please check out: http://gherkin.co.nz/refresh/ and click on the purple box that says 'Membership chest'
Please check my website out: http://www.budgie.richardcmpage.com/index.php I've tried all sorts and can't get the
Please check the following example: http://www.esaer.com.br/csstest/ If the vertical scrollbar doesn't appear, please resize
I got the following code and before sending i check the fields are populated
When delete executes the program crashes. I tried following code to check for corrupted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.