'Wpf TextBox logger - Automatically update

I am using an TextBox in WPF as logger.

I write a message for each step

internal void WriteInfoMessage(int lognr, string v)
{
    try
    {
        tb_log.Text += "\n" + v;
    }
    catch (Exception ex)
    {
         System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate { tb_log.Text += "\n" + v; });
                tb_log.ScrollToEnd();
                tb_log.BeginChange();
    }
}

Problem is that it is update when a MessageBox is coming or when main method is finished. How can I update it every logging?

Main method piece of code

public void MainMethod()
{
    controller.WriteInfoMessage(Lognr, $"Vorlage holen von systemCFG [MH]TemplatePositionName");
    string position_TemplateName = sys.GetProfileString("MH", "TemplatePositionName", "TestTemplatePosition");
    Iposition_std masterObj = controller.GetOrderTemplate(position_TemplateName).masterobject as Iposition_std;
    controller.WriteInfoMessage(Lognr, $"Masterobjekt ist -> {masterObj}");
        
    string topOrderName = controller.GetTagName(doc, "wbs");
    log.WriteInfo(Lognr, $"TopOrder name is : {topOrderName}");
    controller.WriteInfoMessage(Lognr, $"Projekt ->  {topOrderName}");
        
    string basicorderName = controller.GetTagName(doc, "cs");
    log.WriteInfo(Lognr, $"basicorder name is : {basicorderName}");
    controller.WriteInfoMessage(Lognr, $"Auftrag ->  {basicorderName}");
        
    string positionName = controller.GetTagName(doc, "id");
    log.WriteInfo(Lognr, $"Position name is : {basicorderName}");
    controller.WriteInfoMessage(Lognr, $"Position (Leitschaufel) ->  {positionName}");
        
    createdPosition = controller.CreateStrucure(masterObj, ConfigFile, positionName);
    log.WriteInfo(Lognr, $"New position created : {createdPosition}");
    controller.WriteInfoMessage(Lognr, $"Neue Position (Leitschaufel) wurde erstellt {createdPosition}");
}


Solution 1:[1]

I had this problem a few weeks ago, i tried a lot but most didn't work for my needs, problem was i wanted to be able to use logger from any class, interface etc and print out to textbox / richtextbox. i figured out that you could use a eventarg in the main thread of your logger window and call Logging.Write from anywhere and it will work, here is the sample code i made. hope it help https://github.com/Helix-Development/Logging-Sample

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sye