之前寫過一篇 Elmah 整合 HipChat 的文,但因為 Slack 比較潮(其實是因為它說它會開發 WP App 所以我才跳過來)所以就來改一下,讓 Elmah 可以發送到 Slack 。
Elmah 是支援 Http Get 發送的,只是當初設計是給 Tweet 所以命名是「errorTweet」但不要在乎名字,直接拿來用就好了,首先請至 Slack 申請 API Token 「點我」視窗移動到最下方即可申請。

然後移動到你的 Web.config 確定你目前有宣告該有的 errorTweert
<configSections>
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
<section name="errorTweet" requirePermission="false" type="Elmah.ErrorTweetSectionHandler, Elmah" />
</sectionGroup>
</configSections>
然後在 system.web 節點內的 httpModules 也需要加入
<system.web>
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
<add name="ErrorTweet" type="Elmah.ErrorTweetModule, Elmah" />
</httpModules>
最後 system.webServer 節點內的 modules 也請確定有
<system.webServer>
<modules>
<remove name="FormsAuthenticationModule" />
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
<add name="ErrorTweet" type="Elmah.ErrorTweetModule, Elmah" preCondition="managedHandler" />
</modules>
如果你確定只會放在 IIS7以上的話其實就設定 system.webServer 即可。
最後在 elmah 內給它發送的網址即可
<elmah>
<security allowRemoteAccess="true" />
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="elmah-sqlserver" />
<errorTweet maxStatusLength="100" url="https://slack.com/api/chat.postMessage" formFormat="token=9999-9999-9999-9999&channel=#channelName&text={0}&username=elmah"/>
</elmah>
依據上面的方式設定好後,只要 Elmah 接收到訊息就會同時發送到 Slack 內,十分的方便!









回應討論