Skip to main content
 首页 » 编程设计

c#之后台工作人员更新用户界面上的日志

2024年02月05日11webabcd

我有一个 wpf 应用程序,在主窗口中有一个文本框,应该用于在用户运行较长的进程时显示日志记录信息。

<TextBox Grid.Row="1" Margin="10,10,10,10" AcceptsReturn="True" Name="txtLogging" TextWrapping="WrapWithOverflow"  
                 Text="{Binding Path=LogText, Mode=TwoWay}" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" /> 
 
public string LogText 
{ 
    get { return _logText; } 
    set 
    { 
        _logText = value; 
        OnPropertyChanged(); 
    } 
} 

用户界面上的一个按钮会启动至少需要 30 秒,有时长达几个小时的过程。不用说,最好在后台 worker 上运行它。问题是程序中的日志记录类是在 UI 线程上创建的,必须在 worker 执行期间访问以使用当前发生的日志更新 UI。

记录器看起来像这样;

using System; 
using System.IO; 
 
namespace BatchInvoice 
{ 
    public enum LoggingLevel 
    { 
        Verbose = 0, 
        Info = 1, 
        Warning = 2, 
        Error = 3 
    } 
    public sealed class Logger 
    { 
 
        string _logFile; 
        static Logger() { } 
        public bool LogToDataBase = false; 
        public bool LogToFile = true; 
        public bool LogToScreen = false; 
        private Logger() 
        { 
            //string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 
            string filePath = Directory.GetCurrentDirectory(); 
            filePath = filePath + @"\LogFiles"; 
            string extension = ".log"; 
            if (!Directory.Exists(filePath)) 
            { 
                Directory.CreateDirectory(filePath); 
            } 
            /*string currentDir = Environment.CurrentDirectory; 
            DirectoryInfo directory = new DirectoryInfo(currentDir); 
            string fullDirectory = directory.FullName;*/ 
            string date = (DateTime.Now).ToString("yyyyMMddHHmmss"); 
            _logFile = filePath + "\\" + date + extension; 
            minimumLoggingLevel = LoggingLevel.Info; 
        } 
        private LoggingLevel minimumLoggingLevel; 
        public static void SetMinimumLoggingLevel(LoggingLevel minimum) 
        { 
            Instance.minimumLoggingLevel = minimum; 
        } 
        public static LoggingLevel GetMinimumLoggingLevel() 
        { 
            return Instance.minimumLoggingLevel; 
        } 
        private static readonly Logger instance = new Logger(); 
        public static Logger Instance 
        { 
            get 
            { 
                return instance; 
            } 
        } 
        public static void Write(string content) 
        { 
            using (StreamWriter fileWriter = File.AppendText(Instance._logFile)) 
            { 
                fileWriter.WriteLine(content); 
            } 
        } 
        public static void Write(string content, LoggingLevel warningLevel) 
        { 
            if (Instance.minimumLoggingLevel <= warningLevel) 
            { 
                if (Instance.LogToFile) 
                { 
                    using (StreamWriter fileWriter = File.AppendText(Instance._logFile)) 
                    { 
                        fileWriter.WriteLine(warningLevel.ToString() + ": " + content); 
                    } 
                } 
                if (Instance.LogToScreen) 
                    ScreenLogging.Write(content, warningLevel); 
                if (Instance.LogToDataBase) 
                { 
                    //enter database loggign code here. 
                } 
            } 
        } 
    } 
} 
 
using System.Windows; 
using System.Windows.Controls; 
 
namespace BatchInvoice 
{ 
    public class ScreenLogging 
    { 
        private static ScreenLogging _instance; 
        private ScreenLogging() { } 
        public static ScreenLogging Instance 
        { 
            get 
            { 
                if(_instance == null) 
                { 
                    _instance = new ScreenLogging(); 
                } 
                return _instance; 
            } 
        } 
        private TextBox _target; 
        public static void SetTarget(TextBox target) 
        { 
            Instance._target = target; 
        } 
        public static void Write(string content, LoggingLevel warningLevel) 
        { 
            //MessageBox.Show(content, warningLevel.ToString()); 
            Instance._target.AppendText(warningLevel.ToString() + ": " + content + "\n"); 
        } 
    } 
} 

(是的,屏幕记录被分成不同的类是有原因的,但我真的希望我不必改变它)我能做些什么来使对这个记录类的调用反射(reflect)在 UI 中后台 worker ?我应该更改 LogText 属性以从外部文件或类似的内容中读取吗?目前我没有实现后台 worker ,所以日志只在任务完成后显示,但我需要能够在它运行时监控它的进度。当我尝试将它放入后台工作程序时,它在遇到一行试图访问记录器的代码时出错。

请您参考如下方法:

由于您正尝试从另一个线程更新 UI,因此您必须以特殊方式执行此操作,其中线程必须同步以在它们之间传输数据。换句话说,这就像 BackgroundWorker 需要暂停以更新 UI。可以使用 BackgroundWorker 的 ProgressChanged 事件和 ReportProgress 方法来完成。这是一个简单的例子:

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
        // I guess this is how you are using your logger, right? 
        ScreenLogging.SetTarget(this.txtLogging); 
 
        BackgroundWorker worker = new BackgroundWorker(); 
 
        // Your classic event to do the background work... 
        worker.DoWork += Worker_DoWork; 
 
        // Here you can sender messages to UI. 
        worker.ProgressChanged += Worker_ProgressChanged; 
 
        // Don't forget to turn this property to true. 
        worker.WorkerReportsProgress = true; 
 
        worker.RunWorkerAsync(); 
    } 
 
    private void Worker_DoWork(object sender, DoWorkEventArgs e) 
    { 
        var worker = sender as BackgroundWorker; 
 
        Thread.Sleep(3000); 
 
        // ReportProgress sends two values to the ProgressChanged method, for the 
        // ProgressChangedEventArgs object. The first one is the percentage of the  
        // work, and the second one can be any object that you need to pass to UI. 
        // In a simple example, I am passing my log message and just putting  
        // any random value at progress, since it does not matter here. 
        worker.ReportProgress(0, "Test!"); 
    } 
 
    private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
        // Here you get your UserState object, wich is my string message passed on  
        // with the ReportProgress method above. 
        var message = e.UserState as string; 
 
        // Then you call your log as always. Simple, right? 
        ScreenLogging.Write(message, LoggingLevel.Info); 
    }