You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Prism 6.3迁移至8.1 WPF应用:日志功能适配及集成疑问

Prism 8.1日志相关问题解答

Prism 8.1是否自带日志功能?

Prism 8.1没有自带日志功能。从Prism 7.x版本开始,官方移除了内置的日志抽象层与默认实现,转为推荐开发者根据项目需求自行集成第三方日志框架。

集成Microsoft.Practices.EnterpriseLibrary.Logging的步骤

Prism 8中已废弃PrismBootstrapper类,改用PrismApplication作为WPF应用的启动基类,以下是适配的集成步骤:

  • 安装NuGet包
    安装Microsoft.Practices.EnterpriseLibrary.Logging及其依赖的Microsoft.Practices.EnterpriseLibrary.Common包。

  • 封装日志服务
    实现一个适配依赖注入的日志包装类,统一日志调用接口:

    public interface ILogger
    {
        void Debug(string message);
        void Error(string message, Exception ex);
        // 可根据需求扩展Info、Warn等其他级别方法
    }
    
    public class EnterpriseLibraryLogger : ILogger
    {
        private readonly LogWriter _logWriter;
    
        public EnterpriseLibraryLogger(LogWriter logWriter)
        {
            _logWriter = logWriter;
        }
    
        public void Debug(string message)
        {
            _logWriter.Write(message, "Debug", (int)Priority.Low);
        }
    
        public void Error(string message, Exception ex)
        {
            var logEntry = new LogEntry
            {
                Message = message,
                Category = "Error",
                Priority = (int)Priority.High,
                ExtendedProperties = { { "ExceptionDetails", ex.ToString() } }
            };
            _logWriter.Write(logEntry);
        }
    }
    
  • 在PrismApplication中注册服务
    继承PrismApplication并重写RegisterTypes方法,完成日志服务的初始化与注入注册:

    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }
    
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            // 初始化EnterpriseLibrary LogWriter
            var logWriter = new LogWriterFactory().Create();
            // 注册单例LogWriter与自定义日志服务
            containerRegistry.RegisterInstance(logWriter);
            containerRegistry.Register<ILogger, EnterpriseLibraryLogger>();
        }
    }
    
  • 配置日志规则
    在App.config中添加EnterpriseLibrary的日志配置,定义日志输出格式、存储位置等:

    <configuration>
      <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" />
      </configSections>
      <loggingConfiguration tracingEnabled="true" defaultCategory="General">
        <listeners>
          <!-- 配置滚动文件日志监听者 -->
          <add name="RollingFileListener" 
               type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
               fileName="AppLogs\app.log"
               formatter="TextFormatter"
               rollInterval="Day"
               maxArchivedFiles="15" />
        </listeners>
        <formatters>
          <!-- 定义日志输出格式 -->
          <add name="TextFormatter" 
               type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
               template="[{timestamp:yyyy-MM-dd HH:mm:ss}] [{category}] {message}{newline}Exception: {exception}{newline}" />
        </formatters>
        <categorySources>
          <!-- 为不同日志类别绑定监听者 -->
          <add switchValue="All" name="Debug">
            <listeners><add name="RollingFileListener" /></listeners>
          </add>
          <add switchValue="All" name="Error">
            <listeners><add name="RollingFileListener" /></listeners>
          </add>
        </categorySources>
      </loggingConfiguration>
    </configuration>
    
  • 在业务代码中使用日志
    通过构造函数注入ILogger,直接调用日志方法:

    public class MainViewModel : BindableBase
    {
        private readonly ILogger _logger;
    
        public MainViewModel(ILogger logger)
        {
            _logger = logger;
            _logger.Debug("MainViewModel 已初始化");
        }
    
        public void ExecuteOperation()
        {
            try
            {
                // 业务逻辑代码
            }
            catch (Exception ex)
            {
                _logger.Error("执行操作时出错", ex);
            }
        }
    }
    

内容的提问来源于stack exchange,提问作者Balaji G

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 23:20:37