Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a method.
C#
public void AddForegroundColor(ColoredConsoleTarget coloredConsoleTarget)
       {
           var consoleColors = _loggerModel.console_color;
           List<ConsoleOutputColor> consoleOutputColorList = new List<ConsoleOutputColor>();
           ConsoleOutputColor traceForegroundColor;
           ConsoleOutputColor infoForegroundColor;
           ConsoleOutputColor debugForegroundColor;
           ConsoleOutputColor warnForegroundColor;
           ConsoleOutputColor errorForegroundColor;
           ConsoleOutputColor fatalForegroundColor;
           ConsoleOutputColor.TryParse(consoleColors.TRACE, true, out traceForegroundColor);
           ConsoleOutputColor.TryParse(consoleColors.INFO, true, out infoForegroundColor);
           ConsoleOutputColor.TryParse(consoleColors.DEBUG, true, out debugForegroundColor);
           ConsoleOutputColor.TryParse(consoleColors.WARN, true, out warnForegroundColor);
           ConsoleOutputColor.TryParse(consoleColors.ERROR, true, out errorForegroundColor);
           ConsoleOutputColor.TryParse(consoleColors.FATAL, true, out fatalForegroundColor);
           consoleOutputColorList.Add(traceForegroundColor);
           consoleOutputColorList.Add(infoForegroundColor);
           consoleOutputColorList.Add(debugForegroundColor);
           consoleOutputColorList.Add(warnForegroundColor);
           consoleOutputColorList.Add(errorForegroundColor);
           consoleOutputColorList.Add(fatalForegroundColor);
           AddConsoleRowHighlightingRule(coloredConsoleTarget, consoleOutputColorList);
       }

I felt it is dirty, can we clean it? I guess using for loop or so?
Posted
Comments
[no name] 6-Jul-15 16:40pm    
Not seeing where you think a loop would help anything. Using an object initializer for your list would but a loop...?

1 solution

Assuming that consoleColors item are of type ConsoleColorType, something like that could be used:

C#
 public void AddForegroundColor(ColoredConsoleTarget coloredConsoleTarget)
{
    var consoleColors = _loggerModel.console_color;
    var colorTypes = new ConsoleColorType[]
    {
        consoleColors.TRACE,
        consoleColors.INFO,
        consoleColors.DEBUG, 
        consoleColors.WARN,
        consoleColors.ERROR,
        consoleColors.FATAL,
    };


    var consoleOutputColorList = new List<consoleoutputcolor>();

    foreach (var item in colorTypes)
    {
        ConsoleOutputColor traceColor;
        ConsoleOutputColor.TryParse(item, true, out traceColor);

        consoleOutputColorList.Add(traceColor);
    }

    AddConsoleRowHighlightingRule(coloredConsoleTarget, consoleOutputColorList);
}

Make any necessary adjustment depending on color types.

Update: Since strings are used, the following line
C#
var colorTypes = new ConsoleColorType[]

should be change for
C#
var colorTypes = new string[]
 
Share this answer
 
v3
Comments
[no name] 7-Jul-15 10:16am    
The thing is I don't know the type of consoleColors.
private Console_ColorModel consoleColors

public class Console_ColorModel
{
public string TRACE { get; set; }
public string INFO { get; set; }
public string DEBUG { get; set; }
public string WARN { get; set; }
public string ERROR { get; set; }
public string FATAL { get; set; }
}

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900