代码之家  ›  专栏  ›  技术社区  ›  Eric Brown - Cal

我犯了罪……运行时重新写入web.config…用于elmah

  •  1
  • Eric Brown - Cal  · 技术社区  · 15 年前

    我犯了很多罪。我希望有人能告诉我一个更好的方法来重新写这个C。

    我接到一个任务,在运行时修改web.config的一个部分,删除一个elmah错误电子邮件的主题部分,并插入框名。

    原因是我们不能信任我们的CM人员来一致地获得这些权利。 所以我们浪费时间在错误的机器上调试错误。

    所以当我前面有个丑八怪的时候,我开始写…

    这是web.config中我试图修改的部分

     <elmah>
        <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/ELMAH"  />
        <errorMail from="..." to="..."
          subject="Application: EditStaff_MVC,  Environment:Dev, ServerBoxName: AUST-DDEVMX90FF"
          async="true" />
      </elmah>
    

    这是密码。

    private void UpdateElmahErrorEmailSubject( string appPath )
    {
        string machineName = System.Environment.MachineName;
    
        //System.Collections.IDictionary config = ( System.Collections.IDictionary ) ConfigurationManager.GetSection( "elmah" ); ;
        //System.Configuration.Configuration config2 = ( System.Configuration.Configuration ) ConfigurationManager.GetSection( "elmah/errorMail" );
    
        System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration( appPath );
        if ( config == null )
        {
            return;
        }
    
        ConfigurationSectionGroup sectionGroup  = config.GetSectionGroup( "elmah" );
        ConfigurationSection section            = config.GetSection( "elmah/errorMail" );
    
        // i was not able to get directly to the subject, so I had to write it as xml
        string s = section.SectionInformation.GetRawXml();
    
        string search = "ServerBoxName:";
    
        //here is where i started to feel dirty, direct string parsing.
        int startIndex      = s.IndexOf( search );
        int endIndex        = s.IndexOf( "\"", startIndex );
        string toReplace    = s.Substring( startIndex, ( endIndex - startIndex ) );
        s                   = s.Replace( toReplace, search + " " + machineName );
    
        section.SectionInformation.SetRawXml( s );
    
        config.Save();
    }
    

    任何人都能绕过字符串分析吗?我尝试将其作为XML进行处理,但仍然在解析主题时使用了字符串。 有更好的方法吗?

    谢谢,

    埃里克-

    3 回复  |  直到 10 年前
        1
  •  3
  •   Srikar Doddi    15 年前

    使用工厂方法在运行时动态创建httphandler。根据我上次使用的一组HTTP处理程序,创建作为elmah所需的httphandler的自定义变体。

    看看这个例子:

    http://www.informit.com/articles/article.aspx?p=25339&seqNum=5

        2
  •  3
  •   Atif Aziz    10 年前

    在运行时修改配置XML听起来有点过分了。相反,我会使用已经嵌入埃尔玛的钩子。例如,Elmah在发送错误邮件时触发事件。我建议你读一下 Customizing ELMAH’s Error Emails 博客条目 Scott Mitchell 为了一些背景。您可以为 Mailing 事件在你 Global.asax 在邮件准备好但Elmah发送之前操纵主题行。我也会 补丁 主题的相关部分使用 Regex 如下面的示例所示(它比搜索字符串、索引和自己替换部分更强大):

    void ErrorMail_Mailing(object sender, Elmah.ErrorMailEventArgs args)
    {
        args.Mail.Subject = 
            Regex.Replace(args.Mail.Subject, 
                          @"(?<=\bServerBoxName *: *)([_a-zA-Z0-9-]+)", 
                          Environment.MachineName);
    }
    
        3
  •  1
  •   quillbreaker    15 年前

    将其作为XML加载并拉入主题,然后用string解析主题。拆分两次,第一次是“”,然后每个结果字符串都是“:”。您将得到一个数组列表,如:

    第一次分裂: 数组 0应用程序:editstaff_mvc 1环境:开发 2服务器箱名称:aust-ddevmx90ff

    第二(内部)分裂: 数组 0应用 1编辑人员MVC

    在第二个拆分中,如果第一个值是serverbox名称,则使用您的machinename重写第二个值。在执行过程中重新生成字符串。