0
votes

We're using log4mongo-net to save log4net issues in mongo db. Now we've made a simple page where we can browse the logs directly from the browser by reading it untyped from mongo db.

But, when there is a log with for example a POST containing an object, this operation will fail with "Unknown discriminator value 'SomeModel'".

Can we somehow tell Mongo to ignore the Discriminator for the routedata->ActionArguments->model and just deserialize it as untyped as well?

We're using mongocsharpdriver 2.4.3.

This is the concept of how we fetch data:

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

Json-data:

{"_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}

Stacktrace when using BsonDocument instead of object:

at MongoDB.Bson.BsonValue.System.IConvertible.ToType(Type conversionType, IFormatProvider provider at Newtonsoft.Json.JsonWriter.WriteValue(JsonWriter writer, PrimitiveTypeCode typeCode, Object value at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializePrimitive(JsonWriter writer, Object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract 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 member, JsonContainerContract containerContract, JsonProperty containerProperty at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract 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 member, JsonContainerContract 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 member, JsonContainerContract containerContract, JsonProperty containerProperty 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(Type type, Object value, Stream writeStream, Encoding effectiveEncoding at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- Slut på stackspårningen från föregående plats där ett undantag utlöstes --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task at System.Web.Http.WebHost.HttpControllerHandler.d__1b.MoveNext()

Thanks in advance!

1

1 Answers

0
votes

The following should work:

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