【问题标题】:ActionFilterAttribute OnActionExecutedAsync save logger object to databaseActionFilterAttribute OnActionExecutedAsync 将记录器对象保存到数据库
【发布时间】:2015-07-17 15:38:26
【问题描述】:

我正在使用全局 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.

【问题讨论】:

  • 我实际上并没有看到db 来自哪里?您能否在初始化 DbContext 的位置包含代码?
  • @timothyclifford - 添加了整个班级

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


【解决方案1】:

这解决了问题。它没有处理数据库上下文。

    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();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多