我對如何在ASP.NET 5/MVC 6項目中使用Elmah有點困惑。我從nuget那裏得到了這個軟件包,它在project.json中添加了"Elmah.Mvc": "2.1.2"
到dependency。如何在ASP.NET 5/vNext/Core中使用Elmah?
我不知道該從哪裏出發 - 當天回來,nuget會將條目添加到web.config中,現在已經不存在了。我似乎無法在他們的github或其他地方找到任何示例。
我是否缺少一些簡單的東西?
我對如何在ASP.NET 5/MVC 6項目中使用Elmah有點困惑。我從nuget那裏得到了這個軟件包,它在project.json中添加了"Elmah.Mvc": "2.1.2"
到dependency。如何在ASP.NET 5/vNext/Core中使用Elmah?
我不知道該從哪裏出發 - 當天回來,nuget會將條目添加到web.config中,現在已經不存在了。我似乎無法在他們的github或其他地方找到任何示例。
我是否缺少一些簡單的東西?
不是使用ELMAH,而是手動實現錯誤日誌記錄並不困難。此過程將捕獲項目中發生的任何異常並將其記錄到數據庫表中。要做到這一點,添加以下的配置方法Startup.cs
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler(builder =>
{
builder.Run(async context =>
{
context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
context.Response.ContentType = "text/html";
var error = context.Features.Get<Microsoft.AspNetCore.Diagnostics.IExceptionHandlerFeature>();
if (error != null)
{
LogException(error.Error, context);
await context.Response.WriteAsync("<h2>An error has occured in the website.</h2>").ConfigureAwait(false);
}
});
});
}
在Startup.cs包含此還有:
private void LogException(Exception error, HttpContext context)
{
try
{
var connectionStr = Configuration["ConnectionString"];
using (var connection = new System.Data.SqlClient.SqlConnection(connectionStr))
{
var command = connection.CreateCommand();
command.CommandText = @"INSERT INTO ErrorLog (Application, Host, Type, Source, Path, Method, Message, StackTrace, [User], WhenOccured)
VALUES (@Application, @Host, @Type, @Source, @Path, @Method, @Message, @StackTrace, @User, @WhenOccured)";
connection.Open();
if (error.InnerException != null)
error = error.InnerException;
command.Parameters.AddWithValue("@Application", this.GetType().Namespace);
command.Parameters.AddWithValue("@Host", Environment.MachineName);
command.Parameters.AddWithValue("@Type", error.GetType().FullName);
command.Parameters.AddWithValue("@Source", error.Source);
command.Parameters.AddWithValue("@Path", context.Request.Path.Value);
command.Parameters.AddWithValue("@Method", context.Request.Method);
command.Parameters.AddWithValue("@Message", error.Message);
command.Parameters.AddWithValue("@StackTrace", error.StackTrace);
var user = context.User.Identity?.Name;
if (user == null)
command.Parameters.AddWithValue("@User", DBNull.Value);
else
command.Parameters.AddWithValue("@User", user);
command.Parameters.AddWithValue("@WhenOccured", DateTime.Now);
command.ExecuteNonQuery();
}
}
catch { }
}
請注意,您必須在數據庫中創建一個表與此功能中使用的結構。
嗯,當我拋出一個新的例外時,似乎沒有火......我不明白這是如何工作的? –
只是猜測,但它是因爲你在開發模式下嘗試這個嗎?如果是這樣的話,它將在網頁上顯示錯誤細節,而不是調用LogException。這可以通過第一個代碼塊中的if語句進行更改。 – Rono
ELMAH尚未更新爲支持ASP.NET Core。 Atif Azis做了一些工作,構建了一個名爲Bootstrapper的web.config免費配置模塊。 Bootstrapper不支持ASP.NET Core(據我所知)。但我很肯定,只要我們接近RTM,即將開始支持新版本的工作。
如果你問我,一個模塊的名字很愚蠢'Bootstrapper',因爲這是熟悉的JS和CSS庫。 – ProfK
並非所有軟件包都已更新爲與ASP.NET Core兼容。考慮到Elmah和ASP.NET堆棧之間所需的集成水平,我會說Elmah還沒有更新。 – mason
[ELMAH on ASP.NET vNext?]可能的副本(http://stackoverflow.com/questions/28907017/elmah-on-asp-net-vnext) – blowdart