【发布时间】:2018-05-16 21:38:48
【问题描述】:
我们正在使用log4mongo-net 将 log4net 问题保存在 mongo db 中。 现在我们制作了一个简单的页面,我们可以通过从 mongo db 中读取未键入的日志,直接从浏览器中浏览日志。
但是,当日志中包含例如包含对象的 POST 时,此操作将失败,并显示“Unknown discriminator value 'SomeModel'”。
我们能否以某种方式告诉 Mongo 忽略 routedata->ActionArguments->model 的鉴别器,并将其反序列化为无类型?
我们正在使用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 代替对象时的 Stacktrace:
在 MongoDB.Bson.BsonValue.System.IConvertible.ToType(Type conversionType, IFormatProvider provider 在 Newtonsoft.Json.JsonWriter.WriteValue(JsonWriter writer, PrimitiveTypeCode typeCode, Object value 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializePrimitive(JsonWriter 编写器,对象值,JsonPrimitiveContract 合同,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,对象值,JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,对象值,JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter 编写器,IEnumerable 值,JsonArrayContract 合同,JsonProperty 成员,JsonContainerContract 集合合同,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,对象值,JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,对象值,JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter 编写器,IEnumerable 值,JsonArrayContract 合同,JsonProperty 成员,JsonContainerContract 集合合同,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,对象值,JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter 编写器,IEnumerable 值,JsonArrayContract 合同,JsonProperty 成员,JsonContainerContract 集合合同,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,对象值,JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,对象值,类型 objectType 在 Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,对象值,类型 objectType 在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding EffectiveEncoding 在 System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding EffectiveEncoding 在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(类型类型,对象值,流 writeStream,HttpContent 内容 在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(类型类型,对象值,流 writeStream,HttpContent 内容,TransportContext transportContext,CancellationToken cancelToken)---在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync --- 在 System. Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务 在 System.Web.Http.WebHost.HttpControllerHandler.d__1b.MoveNext()
提前致谢!
【问题讨论】:
标签: c# mongodb mongodb-.net-driver mongodb-csharp-2.0