不幸的是,现在可以挖出的旧补丁与 Elmah 有点过时了。这是我在版本 2.0.15523.27 中记录会话变量所做的工作基于此处找到的旧补丁:https ://storage.googleapis.com/google-code-attachments/elmah/issue-12/comment-5/elmah-sessionVariables 。修补
在 Error.cs 中
导入 System.Web.SessionState
using System.Web.SessionState;
寻找:
private NameValueCollection _serverVariables;
private NameValueCollection _queryString;
private NameValueCollection _form;
private NameValueCollection _cookies;
在下面添加:
private NameValueCollection _sessionVariables;
寻找:
_serverVariables = CopyCollection(request.ServerVariables);
_queryString = CopyCollection(qsfc.QueryString);
_form = CopyCollection(qsfc.Form);
_cookies = CopyCollection(qsfc.Cookies);
在下面添加:
_sessionVariables = CopyCollection(context.Session);
寻找:
public NameValueCollection Cookies
{
get { return FaultIn(ref _cookies); }
}
在下面添加:
/// <summary>
/// Gets a collection representing the session variables captured as part of the diagnostic data
/// </summary>
public NameValueCollection SessionVariables
{
get { return FaultIn(ref _sessionVariables); }
}
寻找:
copy._serverVariables = CopyCollection(_serverVariables);
copy._queryString = CopyCollection(_queryString);
copy._form = CopyCollection(_form);
copy._cookies = CopyCollection(_cookies);
在下面添加:
copy._sessionVariables = CopyCollection(_sessionVariables);
寻找:
private static NameValueCollection CopyCollection(NameValueCollection collection)
在上面添加:
private static NameValueCollection CopyCollection(HttpSessionStateBase sessionVariables)
{
if (sessionVariables == null || sessionVariables.Count == 0)
return null;
var copy = new NameValueCollection(sessionVariables.Count);
for (int i = 0; i < sessionVariables.Count; i++)
copy.Add(sessionVariables.Keys[i], sessionVariables[i].ToString());
return copy;
}
在 ErrorJson.cs 中
寻找:
Member(writer, "queryString", error.QueryString);
Member(writer, "form", error.Form);
Member(writer, "cookies", error.Cookies);
在下面添加:
Member(writer, "sessionVariables", error.SessionVariables);
在 ErrorXml.cs 中
寻找:
case "form" : collection = error.Form; break;
case "cookies" : collection = error.Cookies; break;
在下面添加:
case "sessionVariables": collection = error.SessionVariables; break;
寻找:
WriteCollection(writer, "form", error.Form);
WriteCollection(writer, "cookies", error.Cookies);
在下面添加:
WriteCollection(writer, "sessionVariables", error.SessionVariables);
在 ErrorMailHtmlPage.cshtml 中
寻找:
<p>@(RenderPartial<PoweredBy>())</p>
在上面添加:
@foreach (var collection in
from collection in new[]
{
new
{
Id = "SessionVariables",
Title = "Session Variables",
Items = error.SessionVariables,
}
}
let data = collection.Items
where data != null && data.Count > 0
let items = from i in Enumerable.Range(0, data.Count)
select KeyValuePair.Create(data.GetKey(i), data[i])
select new
{
collection.Id,
collection.Title,
Items = items.OrderBy(e => e.Key, StringComparer.OrdinalIgnoreCase)
}
)
{
<div id="@collection.Id">
<h1>@collection.Title</h1>
<table class="collection">
<tr><th>Name</th>
<th>Value</th></tr>
@foreach (var item in collection.Items)
{
<tr><td>@item.Key</td>
<td>@item.Value</td></tr>
}
</table>
</div>
}
在 Visual Studio 中对 ErrorMailHtmlPage.cshtml 进行更改后,右键单击该文件并“运行自定义工具”以生成 ErrorMailHtmlPage.generated.cs 的代码
在 ErrorDetailPage.cshtml 中
查找(在文件末尾):
@*
}
*@
在上面添加:
@{
var sessioncollection = new
{
Data = error.SessionVariables,
Id = "SessionVariables",
Title = "Session Variables",
};
//
// If the collection isn't there or it's empty, then bail out.
//
if (sessioncollection.Data != null && sessioncollection.Data.Count > 0)
{
var items =
from i in Enumerable.Range(0, sessioncollection.Data.Count)
select new
{
Index = i,
Key = sessioncollection.Data.GetKey(i),
Value = sessioncollection.Data[i],
};
items = items.OrderBy(e => e.Key, StringComparer.OrdinalIgnoreCase);
<div id="@sessioncollection.Id">
<h2>@sessioncollection.Title</h2>
@*
// Some values can be large and add scroll bars to the page
// as well as ruin some formatting. So we encapsulate the
// table into a scrollable view that is controlled via the
// style sheet.
*@
<div class="scroll-view">
<table cellspacing="0" style="border-collapse:collapse;" class="table table-condensed table-striped">
<tr>
<th class="name-col" style="white-space:nowrap;">Name</th>
<th class="value-col" style="white-space:nowrap;">Value</th>
</tr>
@foreach (var item in items)
{
<tr class="@(item.Index % 2 == 0 ? "even" : "odd")">
<td class="key-col">@item.Key</td>
<td class="value-col">@item.Value</td>
</tr>
}
</table>
</div>
</div>
}
}
在 Visual Studio 中对 ErrorDetailPage.cshtml 进行更改后,右键单击该文件并“运行自定义工具”以生成 ErrorDetailPage.generated.cs 的代码
现在您可以构建(我只是使用了项目中包含的 build.cmd 文件)并从 bin 中获取所需的 ddl 文件。
- AntiXssLibrary.dll
- Elmah.AspNet.dll
- 埃尔玛.dll
您现在可能还必须修改项目中的 web.config,以在对 Elmah 的任何引用中包含该版本。如果您使用的是 Resharper,您只需单击其中的每一个并修复它们。(为了避免这种情况,可能应该采取不同的方法,但我不确定,我也不太担心弄清楚)
其中之一的一个例子虽然会改变
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
到
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah.AspNet, Version=2.0.15523.27, Culture=neutral, PublicKeyToken=null" />