日志消息将previously创建日志文件 [英] Log messages going to previously created log file

查看:123
本文介绍了日志消息将previously创建日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的企业库5.0日志记录在我的asp.net网站,

我的的web.config 文件如下:

I am using enterprise library 5.0 logging in my asp.net site,
My web.config file is as follows:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true"/>
    </configSections>
    <loggingConfiguration name="FlatFileLogging" tracingEnabled="true"
        defaultCategory="General">
        <listeners>
            <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                fileName="C:\Logs\2013-06-28 14-21-53.log" header="" footer=""
                formatter="Text Formatter" traceOutputOptions="DateTime" />
        </listeners>
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="{timestamp}, {severity}, {message}" name="Text Formatter" />
        </formatters>
        <categorySources>
            <add switchValue="All" name="General">
                <listeners>
                    <add name="Flat File Trace Listener" />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue="All" name="All Events" />
            <notProcessed switchValue="All" name="Unprocessed Category" />
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
                <listeners>
                    <add name="Flat File Trace Listener" />
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
    <appSettings/>
    <connectionStrings/>
    <system.web>
        <compilation debug="true" targetFramework="4.0">
        </compilation>
        <authentication mode="Windows"/>
        <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>
</configuration>

我使用下面的函数改变日志文件的路径:

I am changing log file path using following function:

public static void SetLogFilePath(string filePath)
{
    //string logdirectory = AppDomain.CurrentDomain.BaseDirectory + "Logs\\";
    //if (!Directory.Exists(logdirectory))
    //    Directory.CreateDirectory(logdirectory);
    //logFilePath = logdirectory + (string.IsNullOrWhiteSpace(txtBatchName.Text) ? "" : (txtBatchName.Text + " ")) + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".log";

    if (!File.Exists(filePath))
        File.Create(filePath);

    ConfigurationFileMap objConfigPath = new ConfigurationFileMap();

    // App config file path.
    string appPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
    objConfigPath.MachineConfigFilename = appPath;

    Configuration entLibConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

    LoggingSettings loggingSettings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);

    TraceListenerData traceListenerData = loggingSettings.TraceListeners.Get("Flat File Trace Listener");
    FlatFileTraceListenerData objFlatFileTraceListenerData = traceListenerData as FlatFileTraceListenerData;

    objFlatFileTraceListenerData.FileName = filePath;

    entLibConfig.Save();
}

每当我更改日志文件路径和日志消息发送到文件,日志没有去新创建的文件。将消息记录到previously设置文件路径。看来,新的设置不会立即反映。

Whenever I change log file path and send log messages to file, the logs do not go to newly created file. Log messages go to previously set file path. seems that new setting is not reflecting immediately.

string path = "C:\\Logs\\" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".log";
SetLogFilePath(path);
Logger.Write(message, "General", 1, 0, System.Diagnostics.TraceEventType.Information); 

如何刷新新的设置code马上?

How to refresh the new settings to code immediately?

推荐答案

你是如何定义立即?如果在执行请求的中间意思呢,我不认为你可以做到这一点通过配置,因为配置将无法刷新该请求。

How do you define "immediately"? If you mean in the middle of the executing request then I don't think you can do that via configuration since the configuration will not be refreshed for that request.

下面是似乎基于博客帖子的企业库编程配置。我不写的配置变回盘,但在内存中修改它来代替。

Here's an implementation that seems to work for me based on the blog post Enterprise Library Programmatic Configuration. I don't write the config change back to disk but change it in memory instead.

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        EnterpriseLibraryContainer.Current.GetInstance<LogWriter>()
            .Write("test", "General");

        string path = "C:\\Logs\\anotherlogfile.log";
        SetLogFilePath(path);

        EnterpriseLibraryContainer.Current.GetInstance<LogWriter>()
            .Write("Another test", "General");
    }

    public void SetLogFilePath(string filePath)
    {
        ConfigurationFileMap objConfigPath = new ConfigurationFileMap();

        // App config file path.
        string appPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
        objConfigPath.MachineConfigFilename = appPath;

        Configuration entLibConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

        LoggingSettings loggingSettings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);

        TraceListenerData traceListenerData = loggingSettings.TraceListeners.Get("Flat File Trace Listener");
        FlatFileTraceListenerData objFlatFileTraceListenerData = traceListenerData as FlatFileTraceListenerData;

        objFlatFileTraceListenerData.FileName = filePath;

        IUnityContainer container = new UnityContainer();
        container.AddNewExtension<EnterpriseLibraryCoreExtension>();

        // Configurator will read Enterprise Library configuration 
        // and set up the container
        UnityContainerConfigurator configurator = new UnityContainerConfigurator(container);

        var loggingXmlConfigSource = new SerializableConfigurationSource();
        loggingXmlConfigSource.Add(LoggingSettings.SectionName, loggingSettings);

        // Configure the container with our own custom logging
        EnterpriseLibraryContainer.ConfigureContainer(configurator, loggingXmlConfigSource);

        // Wrap in ServiceLocator
        IServiceLocator locator = new UnityServiceLocator(container);

        // Release lock(s) on existing file(s)
        EnterpriseLibraryContainer.Current.GetInstance<LogWriter>().Dispose();

        // And set Enterprise Library to use it
        EnterpriseLibraryContainer.Current = locator;
    }
}

public class SerializableConfigurationSource : IConfigurationSource
{
    Dictionary<string, ConfigurationSection> sections = new Dictionary<string, ConfigurationSection>();

    public SerializableConfigurationSource()
    {
    }

    public ConfigurationSection GetSection(string sectionName)
    {
        ConfigurationSection configSection;

        if (sections.TryGetValue(sectionName, out configSection))
        {
            SerializableConfigurationSection section = configSection as SerializableConfigurationSection;

            if (section != null)
            {
                using (StringWriter xml = new StringWriter())
                using (XmlWriter xmlwriter = System.Xml.XmlWriter.Create(xml))
                {
                    section.WriteXml(xmlwriter);
                    xmlwriter.Flush();

                    MethodInfo methodInfo = section.GetType().GetMethod("DeserializeSection", BindingFlags.NonPublic | BindingFlags.Instance);
                    methodInfo.Invoke(section, new object[] { XDocument.Parse(xml.ToString()).CreateReader() });

                    return configSection;
                }
            }
        }

        return null;
    }

    public void Add(string sectionName, ConfigurationSection configurationSection)
    {
        sections[sectionName] = configurationSection;
    }

    public void AddSectionChangeHandler(string sectionName, ConfigurationChangedEventHandler handler)
    {
        throw new NotImplementedException();
    }

    public void Remove(string sectionName)
    {
        sections.Remove(sectionName);
    }

    public void RemoveSectionChangeHandler(string sectionName, ConfigurationChangedEventHandler handler)
    {
        throw new NotImplementedException();
    }

    public event EventHandler<ConfigurationSourceChangedEventArgs> SourceChanged;

    public void Dispose()
    {
    }
}

这篇关于日志消息将previously创建日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆