• 【企业库6】【日志应用程序块】实验3:在运行中重新设置日志配置


    Lab 3: Reconfigure Logging at Runtime 实验3:在运行中重新设置日志配置

    In this lab, you will dynamically change the logging configuration while the application is running. This allows you to make temporary changes to your logging configuration to collect additional information without stopping and starting your application. 在这个实验中,你将在程序运行的时候动态的改变日志的配置。这样可以让你临时的改变日志配置信息来收集额外的信息,不用停止再启动你的程序。

    To begin this exercise, open the EnoughPI.sln file located in the ex03egin folder. 打开ex03egin文件夹里面的EnoughPI.sln文件来开始这个练习。

     

    To create a button in the application to update the configuration 在程序中创建一个按钮来更新配置信息

     

    1. Click on MainForm.cs and Select View|Designer to edit the layout of the form. 单击MainForm.cs文件然后选择菜单中的 视图|设计来编辑窗体的布局。
    2. Select the Toolbox panel at the left of the screen and view the Common Controls section. 在屏幕左侧选择工具箱边栏然后选择通用控件项。

    3. Select the Button control and drag it to the form. Position it in the bottom left corner. 选择 按钮 控件然后拖到窗体上。放在左下角。

    4. Right-click the Button you just added and select the Properties menu item. 右键刚刚添加的按钮然后选择 属性。
    5. Change the Text property to "Update." 在文本属性中写入"Update"。
    6. Select the lightning bolt Events icon to edit the events for the Button. Under the Action category, double-click the blank box next to Click and it will fill in "button1_Click." This generates a method to be executed when the button is clicked. You will update this method in the next task. 选择闪电样子的事件图标,然后编辑按钮的事件。在活动条目下,双击Click旁边的空白处,将会自动填充"button1_Click"。这个自动生成的方法用来执行按钮单击时的操作。你需要在下一个操作中修改这个方法。

    To update the logging configuration 更新日志配置

    1. Add the following using statements to MainForm.cs. 将以下引用添加到MainForm.cs文件
      using EnoughPI.Logging; 
      using Microsoft.Practices.EnterpriseLibrary.Logging; 
      using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners; 
      using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters; 
    2. Create a member variable to track how many times the configuration has been updated and initialize it to zero. 创建一个成员变量来跟踪使用了多少时间来更新配置,用0来初始化这个变量。
      private int numUpdates = 0;

       

    3. Create a method called UpdateConfiguration in MainForm.cs to change the destination of the log message. 在MainForm.cs中创建一个方法"UpdateConfiguration",用来改变日志记录信息的目的地。
       1 public void UpdateConfiguration()
       2 {
       3     numUpdates++;
       4     Logger.Writer.Configure(config =>
       5     {
       6         TextFormatter formatter = new TextFormatter(@"Timestamp:
       7             {timestamp(local)}{newline}Message:
       8             {message}{newline}Category: {category}{newline}Priority:
       9             {priority}{newline}EventId: {eventid}{newline}ActivityId:
      10             {property(ActivityId)}{newline}Severity:
      11             {severity}{newline}Title:{title}{newline}");
      12         var newDest = new FlatFileTraceListener(@"C:Temp	race-" +
      13             numUpdates + @".log", "-----------------------------------",
      14             "----------------------------------------", formatter);
      15         config.SpecialSources.AllEvents.Listeners.Clear();
      16         config.SpecialSources.AllEvents.AddTraceListener(newDest);
      17     });
      18 }

      This function reconfigures your Logger's LogWriter to write to a new destination: a new FlatFileTraceListener whose name reflects the number of times the configuration has been updated. First it clears the Listeners for the Trace category, then adds the new FlatFileTraceListener. 这个函数重新配置了你的Logger的LogWriter来写到新的目的地。一个新的FlatFileTraceListener,其名称反映了配置信息被更新的次数。首先该方法清空了Trace类型的监听器,然后添加了这个新的FlatFileTraceListener。

    4. Call your UpdateConfiguration method from the button1_Click method in MainForm.cs. button1_Click方法里面调用UpdateConfiguration方法。
      private void button1_Click(object sender, EventArgs e) 
      { 
          UpdateConfiguration(); 
      }

    To verify reconfiguration at run time is successful 验证运行时重新配置成功

    1. Select the Debug | Start Without Debugging menu command to run the application. Enter your desired precision (something greater than 300 would be best) and click the Calculate button. 选择菜单中的 调试|开始执行(不调试)命令来启动程序。输入你期望的精度(最好大于300)然后单击Calculate按钮。
    2. You may view the trace files in the C:Temp folder. There should be multiple trace files, detailing the number of times the update button was clicked the file starts at (for example, trace-1.log.) 你可以在C:Temp文件夹下查看追踪的日志文件。这里应该有多个文件,具体的数字与Update按钮背单击时开始的数字有关(例如:trace-1.log)。
    3. Close the application and Visual Studio. 关闭程序和Visual Studio。

    To verify that you have completed the exercise correctly, you can use the solution provided in the ex03end folder. 你可以打开ex03end文件夹中提供的解决方案来验证你是否正确的完成了了练习。

  • 相关阅读:
    qt捕鱼达人
    linux通过源码安装gdb
    打造简易http服务器
    linux缓存同步
    NOI之后的碎碎念
    Validate至少有一个不能为空
    IOI 2020 集训队作业
    P6033 [NOIP2004 提高组] 合并果子 加强版 题解
    P3017 [USACO11MAR]Brownie Slicing G 题解
    LOJ #145. DFS 序 2 题解
  • 原文地址:https://www.cnblogs.com/Arnu/p/loggingblock3.html
Copyright © 2020-2023  润新知