首页 文章

从MongoDB获取无类型数据会给出“未知的鉴别值”

提问于
浏览
0

我们使用log4mongo-net来保存mongo db中的log4net问题 . 现在我们已经创建了一个简单的页面,我们可以直接从浏览器浏览日志,方法是从mongo db中读取它 .

但是,当存在包含例如包含对象的POST的日志时,此操作将失败并显示“未知的鉴别器值'SomeModel'” .

我们能否以某种方式告诉Mongo忽略routedata-> ActionArguments->模型的Discriminator,并将其反序列化为无类型?

我们正在使用mongocsharpdriver 2.4.3 .

这是我们如何获取数据的概念:

var _collection = MongoDbFactory.GetDatabase(_connectionString).GetCollection<object>(_collectionName);
var result = _mongoLogsRepository.Collection.Find(filter).ToList();

JSON数据:

{"_id":"5afbf3ec6f21aa0544647ea0","timestamp":"2018-05-16T09:03:40.492Z","level":"ERROR","thread":"19","logger":"X.Intranet.Common.Logging.Logger","message":"WebApi action failed","exceptiondata":{"type":"System.Data.Entity.Validation.DbEntityValidationException","message":"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.","source":"EntityFramework","stackTrace":"   at System.Data.Entity.Internal.InternalContext.SaveChanges()\r\n   at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()\r\n   at System.Data.Entity.DbContext.SaveChanges()\r\n   at X.Intranet.Data.Contexts.XContext.SaveChanges()\r\n   at X.Intranet.Data.Handlers.ScheduleHandler.AddChange(ScheduleChange change)\r\n   at X.Intranet.Business.Managers.ScheduleManager.LogChange(Nullable`1 scheduleExceptionId, Nullable`1 scheduleExtraId, User currentUser, ScheduleChangeReason reason, Assistant fromAssistant, Assistant toAssistant, DateTimePeriodNullable oldTime, DateTimePeriodNullable newTime, Boolean vacant, Boolean noAssistance, String note)\r\n   at X.Intranet.Business.Managers.ScheduleManager.AddVacantAnswer(WorkingPeriod period, Boolean answer, String comment, User currentUser)\r\n   at X.Intranet.Ui.Areas.App.Controllers.Api.Assistant.AppScheduleVacanyController.Answer(Int32 clientId, AppScheduleVacantShiftRequest model)\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__0.MoveNext()"},"userdata":{"userName":"test.assistent"},"routedata":{"Method":"POST","RequestUrl":"https://xxx.azurewebsites.net:443/app/api/v1/assistant/appschedulevacany/answer?clientId=1786","Controller":"X.Intranet.Ui.Areas.App.Controllers.Api.Assistant.AppScheduleVacanyController","Action":"Answer","ActionArguments":{"clientId":1786,"model":{"_t":"AppScheduleVacantShiftRequest","ShiftItemId":"O:182270:2018-06-13 15:00","Answer":false,"Comment":null}},"ReferrerUrl":null},"data":null}

使用BsonDocument而不是object时的Stacktrace:

在MongoDB.Bson.BsonValue.System.IConvertible.ToType(类型conversionType,在Newtonsoft.Json.JsonWriter.WriteValue的IFormatProvider提供商(JsonWriter作家,PrimitiveTypeCode TYPECODE,在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializePrimitive对象值(JsonWriter作家,对象的值,JsonPrimitiveContract Contract ,JsonProperty构件,JsonContainerContract containerContract,JsonProperty containerProperty在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter作家,对象的值,JsonContract valueContract,JsonProperty构件,JsonContainerContract containerContract,JsonProperty containerProperty在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeObject(JsonWriter作家,对象的值,JsonObjectContract Contract ,JsonProperty构件,JsonContainerContract collectionContract,JsonProperty containerProperty在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter作家,对象值,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer,IEnumerable values,JsonArrayContract contract,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty containerProperty at Newtonsoft.Json.Serialization . JsonSerializerInternalWriter.SerializeValue(JsonWriter编写器,Object值,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty at at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,Js onContainerContract containerContract,JsonProperty containerProperty at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer,IEnumerable values,JsonArrayContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value, JsonContract valueContract,JsonProperty构件,JsonContainerContract containerContract,JsonProperty containerProperty在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter作家,IEnumerable的值,JsonArrayContract Contract ,JsonProperty构件,JsonContainerContract collectionContract,JsonProperty containerProperty在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue( JsonWriter编写器,Object值,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerPro perty at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,Object value,Type objectType at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,Object value,Type objectType at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(型类型,对象的值,流writeStream,编码effectiveEncoding在System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(类型类型,对象的值,流writeStream,编码effectiveEncoding在System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(类型类型System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync中的,Object值,Stream writeStream,HttpContent内容(Type type,Object value,Stream writeStream,HttpContent)content,TransportContext transportContext,CancellationToken cancellationToken)---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的任务任务中,slutpåstackspårningenfrånföregåendeplatsdäruttundantagutlöstes---在System.Web.Http.WebHost.HttpControllerHandler.d__1b.MoveNext()

提前致谢!

1 回答

  • 0

    以下应该有效:

    var _collection = MongoDbFactory.GetDatabase(_connectionString).GetCollection<BsonDocument>(_collectionName);
    

相关问题