0

Edit: I've solved the original problem, but it gives rise to another question

The problem was with this line

&& (logLevel == "All" || logLevel == "Error")

The LINQ query in 'list' generated the following SQL

SELECT 
0 AS [C1], 
N'guid' AS [C2], 
[Extent1].[ErrorId] AS [ErrorId], 
[Extent1].[TimeUtc] AS [TimeUtc]
FROM [ELMAH_Error] AS [Extent1]
WHERE ([Extent1].[TimeUtc] >= @p__linq__0) AND ([Extent1].[TimeUtc] <= @p__linq__1) AND ((N'All' = @p__linq__2) OR (N'Error' = @p__linq__3)) 

The error I was getting (Inner exception = "p_linq_2 : String truncation: max=3, len=5, value='Error'.") was caused by this part of the SQL

(N'All' = @p__linq__2)

When I removed logLevel == "All" from the LINQ query, the error disappeared.

So, my question now is - why is LINQ/SQL attempting to truncate p_linq__2? It is performing a comparison... why did it need to truncate?


Original Question

I'm integrating Elmah and Log4Net with my existing MVC application (following this guide - http://www.codeproject.com/Articles/104112/Log-Reporting-Dashboard-for-ASP-NET-MVC). I'm using SQL CE 4. Handled and unhandled errors ARE being logged to the database, the issue I am having is with the UI to view the errors

I am getting an EntityCommandExecutionException triggered by this line (full method listing below)

return new PagedList<LogEvent>(list, pageIndex, pageSize);
  • Error message = "An error occurred while executing the command definition. See the inner exception for details."
  • Inner exception = "p_linq_2 : String truncation: max=3, len=5, value='Error'."

From what I can see, 'list' is populated OK..

The method where the error originates

public IPagedList<LogEvent> GetByDateRangeAndType(int pageIndex, int pageSize, DateTime start, DateTime end, string logProviderName, string logLevel)
    {
        IQueryable<LogEvent> list = null;

        switch (logProviderName)
        {
            case "All":
                foreach (string providerName in logProviders.Keys)
                {
                    IQueryable<LogEvent> logList = GetProvider(providerName).GetByDateRangeAndType(pageIndex, pageSize, start, end, logLevel);
                    list = (list == null) ? logList : list.Union(logList);
                }
                break;

            default:
                list = GetProvider(logProviderName).GetByDateRangeAndType(pageIndex, pageSize, start, end, logLevel);
                break;
        }

        list = list.OrderByDescending(d => d.LogDate);

        return new PagedList<LogEvent>(list, pageIndex, pageSize);
    }

The method it calls

private ILogReportingRepository GetProvider(string logProviderName)
    {
        string logSourceType = logProviders[logProviderName];

        Type providerType = Type.GetType(logSourceType);

        ILogReportingRepository provider = Activator.CreateInstance(providerType, _context) as ILogReportingRepository;

        return provider;
    }

The chained method

public IQueryable<LogEvent> GetByDateRangeAndType(int pageIndex, int pageSize, DateTime start, DateTime end, string logLevel)
    {
        IQueryable<LogEvent> list = (from a in _context.ELMAH_Error
                                     where a.TimeUtc >= start && a.TimeUtc <= end
                                     && (logLevel == "All" || logLevel == "Error")
                                     select new LogEvent
                                     {
                                         IdType = "guid"
                                       ,
                                         Id = ""
                                       ,
                                         IdAsInteger = 0
                                       ,
                                         IdAsGuid = a.ErrorId
                                       ,
                                         LoggerProviderName = "Elmah"
                                       ,
                                         LogDate = a.TimeUtc
                                       ,
                                         MachineName = a.Host
                                       ,
                                         Message = a.Message
                                       ,
                                         Type = a.Type
                                       ,
                                         Level = "Error"
                                       ,
                                         Source = a.Source,
                                         StackTrace = ""
                                     });

        return list;
    }

Stack trace

[InvalidOperationException: p_linq_2 : String truncation: max=3, len=5, value='Error'.] System.Data.SqlServerCe.SqlCeCommand.FillParameterDataBindings(Boolean verifyValue) +1670 System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options) +397 System.Data.SqlServerCe.SqlCeCommand.ExecuteReader(CommandBehavior behavior) +59 System.Data.SqlServerCe.SqlCeMultiCommand.ExecuteReader(CommandBehavior behavior) +342 System.Data.SqlServerCe.SqlCeMultiCommand.ExecuteDbDataReader(CommandBehavior behavior) +41 System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) +10 System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +437

[EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details.] System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) +507 System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext context, ObjectParameterCollection parameterValues) +675 System.Data.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) +102 System.Data.Objects.ObjectQuery1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +30 System.Linq.Enumerable.Single(IEnumerable1 source) +100 System.Data.Objects.ELinq.ObjectQueryProvider.b__3(IEnumerable1 sequence) +5 System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable1 query, Expression queryRoot) +25 System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +70 System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(Expression expression) +82 System.Linq.Queryable.Count(IQueryable1 source) +233 MvcPaging.PagedList1..ctor(IQueryable1 source, Int32 index, Int32 pageSize, Nullable1 totalCount) +532 IDSM.Repository.LogReportingFacade.GetByDateRangeAndType(Int32 pageIndex, Int32 pageSize, DateTime start, DateTime end,


jag
  • 387
  • 2
  • 14

1 Answers1

0

I've solved my problem, although it gives rise to another question.

The problem was with this line

&& (logLevel == "All" || logLevel == "Error")

The LINQ query in 'list' generated the following SQL

SELECT 
0 AS [C1], 
N'guid' AS [C2], 
[Extent1].[ErrorId] AS [ErrorId], 
[Extent1].[TimeUtc] AS [TimeUtc]
FROM [ELMAH_Error] AS [Extent1]
WHERE ([Extent1].[TimeUtc] >= @p__linq__0) AND ([Extent1].[TimeUtc] <= @p__linq__1) AND ((N'All' = @p__linq__2) OR (N'Error' = @p__linq__3)) 

The error I was getting (Inner exception = "p_linq_2 : String truncation: max=3, len=5, value='Error'.") was caused by this part of the SQL

(N'All' = @p__linq__2)

When I removed logLevel == "All" frmo the LINQ query, the error disappeared.

My next question is - why LINQ/SQL attempting to truncate p_linq__2? It was performing a comparison... why did it need to truncate?

jag
  • 387
  • 2
  • 14