Home > front end >  ColorFul Text For Logging In Wpf C#
ColorFul Text For Logging In Wpf C#

Time:10-31

I have a wpf app for for logging the error, general information and warning. These information also get displayed to the user in a text block of app so user can also see the errors and other information.

But right now the color for the text for error warning and success is hard coded as dark grey.

The job I have to do is to make it displayed in different colors Example (Success should be displayed in green color, Error should be red and general info in dark gray). I tried data binding but I can not achieve this. I need some help in this. Below I am sharing relevant code needed.

MainWindow.Xaml Code

ListView Height="230"
                      ItemsSource="{Binding EventLogList}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <WrapPanel Width="560">
                            <TextBlock Foreground="DarkGray"
                                       Text="{Binding}"
                                       TextWrapping="Wrap"/>
                        </WrapPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

MainWindowViewModel.cs

public ObservableCollection<string> EventLogList
    {
        get => _eventLogList;
        set => Set(ref _eventLogList, value);
    }
protected sealed override void LogInfoEvent(string msg)
    { 

        EventLogList.Insert(0, $"{_logDate} [Info]: {msg}");
        base.LogInfoEvent(msg);
    }

    protected sealed override void LogErrorEvent(string msg, Exception exception = null)
    {
        if (IsScanningMode)
            StopScanningExecuteAsync();

        EventLogList.Insert(0, $"{_logDate} [Error]: {msg}");
        base.LogErrorEvent(msg, exception);
        
    }

CodePudding user response:

First off, change log entry type. I defined a new object. ValueTuple won't work, because its members aren't properties.

public class LogEntry
{
    public string Log { get; private set; }
    public string Level { get; private set; }

    public LogEntry(string log, string level)
    {
        Log = log;
        Level = level
    }
}

public ObservableCollection<LogEntry> EventLogList
{
    get => _eventLogList;
    set => Set(ref _eventLogList, value);
}

protected sealed override void LogInfoEvent(string msg)
{ 
    EventLogList.Insert(0, new LogEntry($"{_logDate} [Info]: {msg}", "Info"));
    base.LogInfoEvent(msg);
}

protected sealed override void LogErrorEvent(string msg, Exception exception = null)
{
    if (IsScanningMode)
        StopScanningExecuteAsync();

    EventLogList.Insert(0, new LogEntry($"{_logDate} [Error]: {msg}", "Error"));
    base.LogErrorEvent(msg, exception);        
}

<ListView Height="230"
          ItemsSource="{Binding EventLogList}">
     <ListView.ItemTemplate>
          <DataTemplate>
              <WrapPanel Width="560">
                  <TextBlock Text="{Binding Log}"
                             TextWrapping="Wrap">
                      <TextBlock.Style>
                          <Style TargetType="TextBlock">
                              <Style.Triggers>
                                  <DataTrigger Binding="{Binding Level}" Value="Error">
                                      <Setter Property="Foreground" Value="Red" />
                                  </DataTrigger>
                                  <DataTrigger Binding="{Binding Level}" Value="Info">
                                      <Setter Property="Foreground" Value="DarkGray" />
                                  </DataTrigger>
                                  <DataTrigger Binding="{Binding Level}" Value="Ok">
                                      <Setter Property="Foreground" Value="LightGreen" />
                                  </DataTrigger>
                              </Style.Triggers>
                          </Style>
                      </TextBlock.Style>
                  </TextBlock>
             </WrapPanel>
         </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
  • Related