How to Keep Log4Net From Truncating Exceptions?
Asked Answered
T

2

6

I'm inserting Log4Net events into a SQL database. The Message and Exception fields are both 8000 characters, but occasionally, an event will come through that is longer than 8000 characters, and the data is getting truncated.

Is there any configurable way to get it to chunk out the events into multiple rows? If not, I'm currently thinking about implementing my own ILog that automatically handles chunking the logging events up so I don't get any truncated data. Does anyone have a better idea?

Edit - Logging Config / Database Column Definition

Here is my current parameter Configuration:

<parameter>
  <parameterName value="@message"/>
  <dbType value="String"/>
  <size value="8000"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%message"/>
  </layout>
</parameter>
<parameter>
  <parameterName value="@exception"/>
  <dbType value="String"/>
  <size value="8000"/>
  <layout type="log4net.Layout.ExceptionLayout"/>
</parameter>

The database tables are defined as such:

[Message] [nvarchar](max) NULL,
[Exeception] [ntext] NULL,
Topside answered 10/7, 2014 at 21:39 Comment(3)
What does your current configuration look like? Is log4net adding full stack trace to database table via exception parameter relevant?Jermaine
@CodeCaster, looks like according to your linq, that just removing the size constraint will result in the full log getting populated. Does that sound right?Topside
Comment on accepted answer: "omitting the size element didn't work for me, but, setting it to -1 did work."Jermaine
M
6

You have to set the size value attribute to -1. This will save the whole exception.

Maduro answered 12/7, 2014 at 7:0 Comment(0)
L
0

Check the parameter size in your stored procedure:

CREATE PROCEDURE [dbo].[Log4net_Insert] 
(
    @Date DATETIME,
    @Thread VARCHAR(255),
    @Level VARCHAR(50),
    @Logger VARCHAR(255),
    @Message VARCHAR(4000),
    @Exception VARCHAR(2000)  /* Set this to MAX to prevent truncation! */
) 
AS

BEGIN

    INSERT INTO [dbo].[Log4Net] 
    ([Date],[Thread],[Level],[Logger],[Message],[Exception]) 
    VALUES (@Date, @Thread, @Level, @Logger, @Message, @Exception)

END

Also, Microsoft recommends that you convert anything using NTEXT to something newer: ntext, text, and image (Transact-SQL)

IMPORTANT! ntext, text, and image data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

Lipocaic answered 24/2, 2017 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.