serilog-ui icon indicating copy to clipboard operation
serilog-ui copied to clipboard

could not open a grid page where there is an SqlException

Open mesuttalebi opened this issue 3 years ago • 11 comments

Error Message: Serilog.Ui.MongoDbProvider.MongoDbLogModel: Ambiguous discriminator 'SqlException'.

MongoDb Version: 4.4.5 Serilog UI MongoDb provider Version: 2.1.2 Serilog UI version: 2.1.2

Possible object in mongodb that causes error maybe as below:

{"_id":{"$oid":"61a8dadd1ab0e48d55b06626"},"Level":"Error","UtcTimeStamp":{"$date":"2021-12-02T14:40:30.436Z"},"MessageTemplate":{"Text":"An exception occurred while iterating over the results of a query for context type '{contextType}'.{newline}{error}","Tokens":[{"_t":"TextToken","StartIndex":0,"Text":"An exception occurred while iterating over the results of a query for context type '"},{"_t":"PropertyToken","StartIndex":84},{"_t":"TextToken","StartIndex":97,"Text":"'."},{"_t":"PropertyToken","StartIndex":99},{"_t":"PropertyToken","StartIndex":108}]},"RenderedMessage":"An exception occurred while iterating over the results of a query for context type '\"AcerPlatformV2.Infrastructure.LoggerDbContext\"'.\"\r\n\"\"Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid object name 'SystemControlLogs'.\r\n   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)\r\n   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)\r\n   at Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData()\r\n   at Microsoft.Data.SqlClient.SqlDataReader.get_MetaData()\r\n   at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)\r\n   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)\r\n   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)\r\n   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)\r\n   at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)\r\n   at Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n   at System.Data.Common.DbCommand.ExecuteReader()\r\n   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)\r\n   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()\r\nClientConnectionId:47fb5428-dd6e-4ff3-a4d4-03d1960792a9\r\nError Number:208,State:1,Class:16\"","Properties":{"contextType":"AcerPlatformV2.Infrastructure.LoggerDbContext","newline":"\r\n","error":"Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid object name 'SystemControlLogs'.\r\n   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)\r\n   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)\r\n   at Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData()\r\n   at Microsoft.Data.SqlClient.SqlDataReader.get_MetaData()\r\n   at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)\r\n   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)\r\n   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)\r\n   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)\r\n   at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)\r\n   at Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n   at System.Data.Common.DbCommand.ExecuteReader()\r\n   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)\r\n   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()\r\nClientConnectionId:47fb5428-dd6e-4ff3-a4d4-03d1960792a9\r\nError Number:208,State:1,Class:16","EventId":{"Id":10100,"Name":"Microsoft.EntityFrameworkCore.Query.QueryIterationFailed"},"SourceContext":"Microsoft.EntityFrameworkCore.Query"},"Exception":{"_t":"SqlException","HelpLink":null,"Source":"Core Microsoft SqlClient Data Provider","HResult":-2146232060,"Message":"Invalid object name 'SystemControlLogs'.","StackTrace":"   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)\r\n   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)\r\n   at Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData()\r\n   at Microsoft.Data.SqlClient.SqlDataReader.get_MetaData()\r\n   at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)\r\n   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)\r\n   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)\r\n   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)\r\n   at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)\r\n   at Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n   at System.Data.Common.DbCommand.ExecuteReader()\r\n   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)\r\n   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()\r\n   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n   at AcerPlatformV2.Business.Services.SystemControlLogService.GetLastLogs(Int32 logCount) in D:\\MT\\Projects\\GatewayProUI.git\\AcerPlatformV2.Business\\Services\\SystemControlLogService.cs:line 108\r\n   at AcerPlatformV2.Presentation.ModelServices.MonitoringModelService.SystemControlErrorLogNotification() in D:\\MT\\Projects\\GatewayProUI.git\\AcerPlatformV2.Presentation\\ModelServices\\MonitoringModelService.cs:line 112\r\n   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()","Data":{"HelpLink-ProdName":"Microsoft SQL Server","HelpLink-ProdVer":"13.00.4001","HelpLink-EvtSrc":"MSSQLServer","HelpLink-EvtID":"208","HelpLink-BaseHelpUrl":"http://go.microsoft.com/fwlink","HelpLink-LinkId":"20476"}}}

mesuttalebi avatar Dec 03 '21 06:12 mesuttalebi

It's strange because Serilog.UI.MongoDbProvider has no dependency on SQL. Could you please tell me how can I reproduce this exception?

mo-esmp avatar Dec 03 '21 14:12 mo-esmp

Hi Mohsen, no there is no dependency on sql, in my project I have sql db also, but I use serilog with mongodb for logging. in project there is an error logged into mongodb as what I added into this issue as json. when serilogui tries to deserialize/serialize this error to show in view, the error is thrown.

for reproducing, please create a mongodb collection and add the that json to logs collection, then try to show it serilog-ui

mesuttalebi avatar Dec 06 '21 11:12 mesuttalebi

Ok, got it what you mean. I will try to reproduce the error with that log message and let you know the result.

mo-esmp avatar Dec 06 '21 11:12 mo-esmp

@mesuttalebi could you please tell me what and which version of MongoDB sink you are using?

mo-esmp avatar Dec 13 '21 08:12 mo-esmp

Hi @mo-esmp,

MongoDB.Driver 2.13.1 Serilog.Sinks.MongoDB version 5.1.1 Serilog.UI.MongoDbProvider 2.1.2

mesuttalebi avatar Dec 16 '21 07:12 mesuttalebi

Hi Mohsen,

I could able to solve the problem with adding a custom serializer for dynamic objects as below:

the reason for error is because of _t value in exception field, because it in every document it may has another inherited type of Exception like SqlException, FormatException etc.

 public class DynamicSerializer : SerializerBase<dynamic> 
        {
            public override dynamic Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
            {
                var myBSONDoc = BsonDocumentSerializer.Instance.Deserialize(context);
                return JsonConvert.DeserializeObject<dynamic>(myBSONDoc.ToString());
            }

            public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
            {
                base.Serialize(context, args, value);
            }
        }

mesuttalebi avatar Dec 22 '21 14:12 mesuttalebi

Sorry, these days I'm quite busy. Here is the solution: Unknown discriminator value "SqlException", How to ignore discriminator on dynamic type. Please checkout branch bug17 and take a look at MongoDbLogModel class. Right now the problem is all those extra fields like "_t" will be sent to the UI and displayed. Need to find a way to ignore such fields or do not send to UI.

mo-esmp avatar Dec 23 '21 06:12 mo-esmp

Hi @mo-esmp,

do you think it's possible to merge to master the bug17 branch? Even if there are extra fields, it would fix the exception retrievals (at the same time, those fields seem interesting to have and I guess they could be kept... as an example, the _t gives info on the actual thrown exception name).

(OT: is there a repo for the frontend of serilog-ui? I searched for them but I found only the compiled assets - I'm not sure if they're publicly available)

followynne avatar Jan 01 '22 23:01 followynne

Hey @followynne Yes, it's possible, at least it's a working solution for now. Please take a look at Serilog.Ui.Web project, it contains codes for UI. To get started check SerilogUiMiddleware.cs, index.html and main.js.

mo-esmp avatar Jan 02 '22 15:01 mo-esmp

Hi @mo-esmp, awesome news, it will be really helpful to check any exceptions! Btw, thanks a lot for the info on UI (and, well, for the cool project of course 👍 )

followynne avatar Jan 04 '22 21:01 followynne

I've merged branch Bug17, please update Serilog.Ui.MongoDbProvider to version 2.2.0.

mo-esmp avatar Jan 07 '22 10:01 mo-esmp