ActionFilterAttribute OnActionExecutedAsync将logger对象保存到数据库

时间:2015-07-17 15:38:26

标签: c# asp.net-web-api2 entity-framework-6.1

我正在使用全局ActionFilterAttribute来插入我的记录器逻辑,我想将记录器对象保存到数据库中。

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public sealed class RequireHttpHeaderAttribute : ActionFilterAttribute
{
    public ApplicationDbContext db = new ApplicationDbContext();

    public override async Task OnActionExecutedAsync(HttpActionExecutedContext httpActionExecutedContext, CancellationToken cancellationToken)
    {
        HttpActionExecutedContext executedContext;
        var WebServiceRequestURI = httpActionExecutedContext.Request.RequestUri;
        var WebServiceRequestMethod = httpActionExecutedContext.Request.Method;
        Task<string> content = httpActionExecutedContext.Request.Content.ReadAsStringAsync();
        string WebServiceRequestBody = content.Result;
        if (WebServiceRequestBody == "")
            WebServiceRequestBody = "No-Content";
        var UserID = httpActionExecutedContext.Request.GetOwinContext().Authentication.User.Claims.First().Value;
        // Get the sender address
        var myRequest = ((HttpContextWrapper)httpActionExecutedContext.Request.Properties["MS_HttpContext"]).Request;
        var IPAddress = myRequest.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (!string.IsNullOrEmpty(IPAddress))
        {
            string[] ipRange = IPAddress.Split(',');
            int le = ipRange.Length - 1;
            string trueIP = ipRange[le];
        }
        else
        {
            IPAddress = myRequest.ServerVariables["REMOTE_ADDR"];
        }

        var webserviceRequestDate = DateTime.Now;
        FMBP_ActionLog fMBP_ActionLog = new FMBP_ActionLog();
        fMBP_ActionLog.WebServiceRequestString = WebServiceRequestURI.ToString();
        fMBP_ActionLog.WebServiceRequestMethod = WebServiceRequestMethod.ToString();
        fMBP_ActionLog.WebServiceRequestBody = WebServiceRequestBody;
        fMBP_ActionLog.WebServiceRequestDate = DateTime.Now;
        fMBP_ActionLog.IPAddress = IPAddress;
        fMBP_ActionLog.UserID = UserID;
        fMBP_ActionLog.StatusCode = (int)httpActionExecutedContext.Response.StatusCode;
        db.FMBP_ActionLog.Add(fMBP_ActionLog);
        db.SaveChanges();
    }
}

将FMBP_ActionLog保存到数据库的最佳方法是什么?我收到了很多这样的随机错误

"ExceptionMessage":"The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state.

1 个答案:

答案 0 :(得分:0)

这解决了这个问题。它没有处理db上下文。

    using (var db = new ApplicationDbContext())
    {
        FMBP_ActionLog fMBP_ActionLog = new FMBP_ActionLog();
        fMBP_ActionLog.WebServiceRequestString = WebServiceRequestURI.ToString();
        fMBP_ActionLog.WebServiceRequestMethod = WebServiceRequestMethod.ToString();
        fMBP_ActionLog.WebServiceRequestBody = WebServiceRequestBody;
        fMBP_ActionLog.WebServiceRequestDate = DateTime.Now;
        fMBP_ActionLog.IPAddress = IPAddress;
        fMBP_ActionLog.UserID = UserID;
        fMBP_ActionLog.StatusCode = (int)httpActionExecutedContext.Response.StatusCode;
        db.FMBP_ActionLog.Add(fMBP_ActionLog);
        // Perform data access using the context 
        db.SaveChanges();
    }