你好,
我有未处理的异常记录器,它会将每个错误记录到表中。实际上,它可以工作,但是当我对超过1000个用户进行负载测试时,情况很少,错误不会写入此表中。 (成功950次,出现50个错误)为什么会发生这种情况?有任何想法吗? 50位用户甚至由于某种原因而没有开始?
我有未处理的异常记录器,它会将每个错误记录到表中。实际上,它可以工作,但是当我对超过1000个用户进行负载测试时,情况很少,错误不会写入此表中。 (成功950次,出现50个错误)为什么会发生这种情况?有任何想法吗? 50位用户甚至由于某种原因而没有开始?
C#:
public class UnhandledExceptionLogger : ExceptionLogger
{
public override void Log(ExceptionLoggerContext context)
{
var ex = context.Exception;
string strLogText = "";
strLogText += Environment.NewLine + "Source ---\n{0}" + ex.Source;
strLogText += Environment.NewLine + "StackTrace ---\n{0}" + ex.StackTrace;
strLogText += Environment.NewLine + "TargetSite ---\n{0}" + ex.TargetSite;
if (ex.InnerException != null)
{
strLogText += Environment.NewLine + "Inner Exception is {0}" + ex.InnerException;//error prone
}
if (ex.Message != null)
{
strLogText += Environment.NewLine + "Message ---\n{0}" + ex.Message;//error prone
}
var requestedURi = (string)context.Request.RequestUri.AbsoluteUri;
var requestMethod = context.Request.Method.ToString();
var timeUtc = DateTime.Now;
SqlErrorLogging sqlErrorLogging = new SqlErrorLogging();
ApiError apiError = new ApiError()
{
Message = strLogText,
RequestUri = requestedURi,
RequestMethod = requestMethod,
TimeUtc = DateTime.Now
};
sqlErrorLogging.InsertErrorLog(apiError);
}
}
C#:
public void InsertErrorLog(ApiError apiError)
{
using (var sqlConnection =
new SqlConnection(ConfigurationManager.ConnectionStrings["GameContext"].ConnectionString))
{
try
{
sqlConnection.Open();
using (SqlCommand cmd =
new SqlCommand(
"INSERT INTO [dbo].[API_Error] ([Message],[RequestMethod],[RequestUri],[TimeUtc]) VALUES (@Message, @RequestMethod, @RequestUri, @TimeUtc)",
sqlConnection))
{
cmd.Parameters.AddWithValue("@TimeUtc", apiError.TimeUtc);
cmd.Parameters.AddWithValue("@RequestUri", apiError.RequestUri);
cmd.Parameters.AddWithValue("@Message", apiError.Message);
cmd.Parameters.AddWithValue("@RequestMethod", apiError.RequestMethod);
cmd.ExecuteNonQuery();
}
}
catch (Exception e)
{
throw e;
}
finally
{
sqlConnection.Close();
}
}
}