log4net inserting (null) into custom column

*爱你&永不变心* 提交于 2020-01-04 05:57:17

问题


Added a custom column as suggested here. Things are working fine when ever I log something into this column using the code line

log4net.GlobalContext.Properties["CustomColumn"] = "Custom value";

But if I pass null instead as below

log4net.GlobalContext.Properties["CustomColumn"] = null;

this is logged as (null)(a string null included in brakets). This also happens if I do not log at all, like I comment the code as shown below.

//log4net.GlobalContext.Properties["CustomColumn"] = null;

So the question is, is there a way to make it actually null in the database, instead of a string - (null). I am using ms sql server 2008 r2


回答1:


try this

log4net.GlobalContext.Properties["CustomColumn"] = DBNull.Value;



回答2:


I have modified the insert statement of the commandText value in Web.config file to

"INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception],[BrowserCapabilities]) VALUES (@log_date, @thread, @log_level, 
        @logger, @message, @exception, CASE WHEN @BrowserCapabilities = '' OR @BrowserCapabilities = '(null)' THEN NULL ELSE @BrowserCapabilities END )" 

Now this works fine.




回答3:


I prefer "NULLIF" over "CASE WHEN" in this case. This alternative still requires modifying the insert statement of the commandText value in the Web.config file, but it is more efficient:

INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception],[BrowserCapabilities]) 
VALUES (@log_date,@thread,@log_level,@logger,@message,@exception,NULLIF(@BrowserCapabilities,'(null)')"


来源:https://stackoverflow.com/questions/28964934/log4net-inserting-null-into-custom-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!