Enterprise Library Fluent API 和滚动日志文件不滚动

发布于 2024-11-17 16:36:19 字数 2428 浏览 7 评论 0原文

我正在使用 Fluent API 来处理使用 EntLib 进行日志记录的各种配置选项。

我正在代码中手动构建loggingConfiguration部分。它似乎工作得很好,只是 RollingFlatFileTraceListener 实际上并不滚动文件。它将遵守大小限制并适当限制写入文件的数据量,但它实际上并不创建新文件并继续记录日志。

我已经使用示例应用程序和 app.config 对其进行了测试,它似乎可以工作。所以我猜我错过了一些东西,尽管似乎需要的每个配置选项都在那里。

以下是代码的基础知识(使用硬编码值来显示似乎不起作用的配置): //为 Fluent API 创建配置构建器 var configBuilder = new ConfigurationSourceBuilder();

            //Start building the logging config section                 
            var logginConfigurationSection = new LoggingSettings("loggingConfiguration", true, "General");                 
            logginConfigurationSection.RevertImpersonation = false;
            var _rollingFileListener = new RollingFlatFileTraceListenerData("Rolling Flat File Trace Listener", "C:\\tracelog.log", "----------------------", "",
                            10, "MM/dd/yyyy", RollFileExistsBehavior.Increment,
                            RollInterval.Day, TraceOptions.None,
                            "Text Formatter", SourceLevels.All);


            _rollingFileListener.MaxArchivedFiles = 2;

            //Add trace listener to current config
            logginConfigurationSection.TraceListeners.Add(_rollingFileListener);

            //Configure the category source section of config for flat file
            var _rollingFileCategorySource = new TraceSourceData("General", SourceLevels.All);

            //Must be named exactly the same as the flat file trace listener above.
            _rollingFileCategorySource.TraceListeners.Add(new TraceListenerReferenceData("Rolling Flat File Trace Listener"));

            //Add category source information to current config
            logginConfigurationSection.TraceSources.Add(_rollingFileCategorySource);          

            //Add the loggingConfiguration section to the config.
            configBuilder.AddSection("loggingConfiguration", logginConfigurationSection);

            //Required code to update the EntLib Configuration with settings set above.
            var configSource = new DictionaryConfigurationSource();
            configBuilder.UpdateConfigurationWithReplace(configSource);

            //Set the Enterprise Library Container for the inner workings of EntLib to use when logging
            EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

任何帮助将不胜感激!

I am using the Fluent API to handle various configuration options for Logging using EntLib.

I am building up the loggingConfiguration section manually in code. It seems to work great except that the RollingFlatFileTraceListener doesn't actually Roll the file. It will respect the size limit and cap the amount of data it writes to the file appropriately, but it doesn't not actually create a new file and continue the logs.

I've tested it with a sample app and the app.config and it seems to work. So I'm guess that I am missing something although every config option that seems like it needs is there.

Here is the basics of the code (with hard-coded values to show a config that doesn't seem to be working):
//Create the config builder for the Fluent API
var configBuilder = new ConfigurationSourceBuilder();

            //Start building the logging config section                 
            var logginConfigurationSection = new LoggingSettings("loggingConfiguration", true, "General");                 
            logginConfigurationSection.RevertImpersonation = false;
            var _rollingFileListener = new RollingFlatFileTraceListenerData("Rolling Flat File Trace Listener", "C:\\tracelog.log", "----------------------", "",
                            10, "MM/dd/yyyy", RollFileExistsBehavior.Increment,
                            RollInterval.Day, TraceOptions.None,
                            "Text Formatter", SourceLevels.All);


            _rollingFileListener.MaxArchivedFiles = 2;

            //Add trace listener to current config
            logginConfigurationSection.TraceListeners.Add(_rollingFileListener);

            //Configure the category source section of config for flat file
            var _rollingFileCategorySource = new TraceSourceData("General", SourceLevels.All);

            //Must be named exactly the same as the flat file trace listener above.
            _rollingFileCategorySource.TraceListeners.Add(new TraceListenerReferenceData("Rolling Flat File Trace Listener"));

            //Add category source information to current config
            logginConfigurationSection.TraceSources.Add(_rollingFileCategorySource);          

            //Add the loggingConfiguration section to the config.
            configBuilder.AddSection("loggingConfiguration", logginConfigurationSection);

            //Required code to update the EntLib Configuration with settings set above.
            var configSource = new DictionaryConfigurationSource();
            configBuilder.UpdateConfigurationWithReplace(configSource);

            //Set the Enterprise Library Container for the inner workings of EntLib to use when logging
            EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

Any help would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-柠檬树下少年和吉他 2024-11-24 16:36:19

您的时间戳模式是错误的。它应该是 yyy-mm-dd 而不是 MM/dd/yyyy。不支持“/”字符。

此外,您可以使用 Fluent 来实现您的目标配置界面更容易。方法如下:

    ConfigurationSourceBuilder formatBuilder = new ConfigurationSourceBuilder();

    ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder();
    builder.ConfigureLogging().LogToCategoryNamed("General").
        SendTo.
        RollingFile("Rolling Flat File Trace Listener")
        .CleanUpArchivedFilesWhenMoreThan(2).WhenRollFileExists(RollFileExistsBehavior.Increment)
        .WithTraceOptions(TraceOptions.None)
        .RollEvery(RollInterval.Minute)
        .RollAfterSize(10)
        .UseTimeStampPattern("yyyy-MM-dd")
        .ToFile("C:\\logs\\Trace.log")
        .FormatWith(new FormatterBuilder().TextFormatterNamed("textFormatter"));

    var configSource = new DictionaryConfigurationSource(); 
    builder.UpdateConfigurationWithReplace(configSource); 
    EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
    var writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

    DateTime stopWritingTime = DateTime.Now.AddMinutes(10);
    while (DateTime.Now < stopWritingTime)
    {
        writer.Write("test", "General");
    }

Your timestamp pattern is wrong. It should be yyy-mm-dd instead of MM/dd/yyyy. The ‘/’ character is not supported.

Also, you could accomplish your objective by using the fluent configuration interface much easier. Here's how:

    ConfigurationSourceBuilder formatBuilder = new ConfigurationSourceBuilder();

    ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder();
    builder.ConfigureLogging().LogToCategoryNamed("General").
        SendTo.
        RollingFile("Rolling Flat File Trace Listener")
        .CleanUpArchivedFilesWhenMoreThan(2).WhenRollFileExists(RollFileExistsBehavior.Increment)
        .WithTraceOptions(TraceOptions.None)
        .RollEvery(RollInterval.Minute)
        .RollAfterSize(10)
        .UseTimeStampPattern("yyyy-MM-dd")
        .ToFile("C:\\logs\\Trace.log")
        .FormatWith(new FormatterBuilder().TextFormatterNamed("textFormatter"));

    var configSource = new DictionaryConfigurationSource(); 
    builder.UpdateConfigurationWithReplace(configSource); 
    EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
    var writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

    DateTime stopWritingTime = DateTime.Now.AddMinutes(10);
    while (DateTime.Now < stopWritingTime)
    {
        writer.Write("test", "General");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文