注意:我知道避免使用会话的各种原因,但这是我继承的项目,所以请跳过任何回复的那部分:)
由于这是一个已解决的问题,我希望有人可以指向ELMAH补丁/分支/分支,其中包括记录会话数据而不是重新发明轮子。
一个奇怪的事情是来自Atif的旧帖子说他们已经登录了:
http://markmail.org/message/ncmdgwm5rmzewbwu
评论者henningst提到在这里添加会话变量:
http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx
另一种方法(我宁愿避免)是将值复制到cookie中
我知道一种替代方法是切换到ELMAH之外的东西(比如Exceptioneer - 请参阅http://exceptioneer.com/Public/ExceptioneerAndELMAH.aspx)但是因为这是我目前ELMAH的唯一问题,我宁愿只修补ELMAH而不是切换到别的什么。
答案 0 :(得分:6)
Atif在twitter上回复说没有已知的补丁:
http://twitter.com/raboof/statuses/7229453423
所以我创建了一个补丁:
http://twitter.com/manningj/statuses/7231616905
http://blog.sublogic.com/2009/12/patch-to-enable-session-variable-logging-with-elmah/
答案 1 :(得分:3)
我没有修补Elmah,而是使用Exception数据。在Global.asax中,我将额外的数据插入到Application_Error()的异常中。 “HistoryStack”是我自己的用于记录用户历史记录的类,包括按钮和标签点击:
void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError().GetBaseException();
var stack = HistoryStack.Dump(); // essentially grabs data from the session
ex.Data.Add("historyStack", stack);
}
然后,在ErrorMail_Mailing()中,我抓回了数据并将其附加到电子邮件中:
void ErrorMail_Mailing(object sender, Elmah.ErrorMailEventArgs e)
{
var stack = e.Error.Exception.Data["historyStack"] as Stack<string>;
if (stack == null && e.Error.Exception.InnerException != null)
{
// could probably skip the first try and go straight to this assignment:
stack = e.Error.Exception.InnerException.Data["historyStack"] as Stack<string>;
}
if (stack != null && stack.Count > 0)
{
e.Mail.Body = e.Mail.Body + "<h1>Browsing History</h1>" + System.Environment.NewLine;
while (stack.Count > 0)
{
e.Mail.Body = e.Mail.Body + stack.Pop() + "<br />" + System.Environment.NewLine;
}
}
}
现在,此数据会附加到电子邮件的底部。没有必要的补丁或扩展。
答案 2 :(得分:0)
不幸的是,可以挖掘的旧补丁现在有点过时了Elmah。这是我在2.0.15523.27版本中记录会话变量所做的工作 基于此处的旧修补程序:https://storage.googleapis.com/google-code-attachments/elmah/issue-12/comment-5/elmah-sessionVariables.patch
在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文件。
您可能还需要立即修改项目中的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" />