Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I don't seem to be able to insert a null into a database column #3380

Closed
alatabbi opened this issue May 8, 2019 · 3 comments
Closed

I don't seem to be able to insert a null into a database column #3380

alatabbi opened this issue May 8, 2019 · 3 comments

Comments

@alatabbi
Copy link

alatabbi commented May 8, 2019

Hi!
I don't seem to be able to insert a null into a database column, below is my code/configs
in columns Venue and DepartmentID, when I set the value/DepartmentID to null, NLog keeps inserting empty string

Am I missing something?

NLog version: (e.g. 4.6.3)

Platform: .Net 4.5

//////////////////////////////////
**NLog version**: (e.g. 4.6.3)
var logger = LogManager.GetLogger("FUT");
var dbTarget = ((AsyncTargetWrapper)LogManager.Configuration.FindTargetByName("FUTLog")).WrappedTarget as DatabaseTarget;

dbTarget.DBHost = "****";
dbTarget.DBDatabase = "****";
dbTarget.DBUserName = "****";
dbTarget.DBPassword = "****";

LogManager.ReconfigExistingLoggers();
LogEventInfo eventInfo = new LogEventInfo();
eventInfo.Properties["DepartmentName"] = "SomeDepartmentName";
eventInfo.Properties["DepartmentID"] = 777;
// eventInfo.Properties["Venue"] = "Venue";
logger .Info(eventInfo); 
//////////////////////////////////

Current NLog config (xml or C#, if relevant)

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >

<targets async="true">
<target name="FUTLog"
                xsi:type="Database"
                dbProvider="System.Data.SqlClient"
                commandType="StoredProcedure"
                commandText="[dbo].[sp_InsertFeatureUsageTracking]">
<parameter name="@FeatureID" layout="${event-properties:item=FeatureID}" />
<parameter name="@CategoryID" layout="${event-properties:item=CategoryID}" />
<parameter name="@DepartmentID" layout="${event-properties:item=DepartmentID}" dbType="${db-null}" />

<parameter name="@Venue" layout="${when:when='${event-properties:item=Venue}' == '':inner=${db-null}:else=${event-properties:item=Venue}}" dbType="${db-null}" />

<parameter name="@EventTimestamp" layout="${date:universalTime=true}" />
</target>
</targets>

    <rules>
        <logger name="FUT"  levels="Info" minlevel="Info" writeTo="FUTLog" />
    </rules>
</nlog>

The expected result is to insert null into columns Venue and DepartmentID

@304NotModified
Copy link
Member

304NotModified commented May 8, 2019

Hi,

Unfortunately ${db-null} isn't supported with ${when} for now in NLog 4.6.

This will be fixed in the upcoming release. See this PR from a few days ago (see #3373). The next planned release is 5.0 and this will take some week/months.

I could try to backport it and create a pre-release, could you test it with a pre-release? (I will sent a URL to the nugetpackage then).

PS: dbType="${db-null} isn't correct. It should be something like dbType="string" (dBType value) or dbType="SqlDbType.NChar" (SQL Server with ADO.NET) see https://github.com/NLog/NLog/wiki/Database-target

@alatabbi
Copy link
Author

I found a way to do it, basically adding

parameterType="System.Object"

to the nullable columns parameters does the trick.

Thanks.

@304NotModified
Copy link
Member

Unfortunately ${db-null} isn't supported with ${when} for now in NLog 4.6.

FYI, this will be fixed in NLog 4.6.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants