Search code examples
c#asp.netnlogjsnlog

Getting JSNLog to log with NLog


I am currently trying to implement a logging system for my ASP.Net application similar to this where Elmah is used to catch all of the .NET Uncaught Exceptions that happen and log them to a SQL Database, with NLog being used for the more lightweight Debug() and Info() calls that will also be logged to a SQL Server. In addition to this I wanted to add JSNLog so that I could send all my Javascript Debug/Info/Errors etc to NLog for storing in the SQL Database too.

I have installed Elmah via the NuGet Package and made the necessary changes to my web.config

<elmah>
  <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ErrorLog"/>
  <security allowRemoteAccess="false" />
</elmah>

<connectionStrings>
  <add
    name="ErrorLog"
    connectionString="ConnectionStringGoesHere"
    providerName="System.Data.SqlClient" />
</connectionStrings>

And I can confirm that Elmah logs to my SQL Database correctly.

I have also installed the NLog and NLog.Config NuGet packages, along with adding two nodes to the NLog.config file

<targets>  
    <!-- database target -->
    <target name="database" 
            xsi:type="Database"
            connectionStringName="NLog"
            commandText="exec dbo.InsertLog
                            @level,
                            @callSite,
                            @type,
                            @message,
                            @stackTrace,
                            @innerException,
                            @additionalInfo">
            <parameter name="@level" layout="${level}" />
            <parameter name="@callSite" layout="${callsite}" />
            <parameter name="@type" layout="${exception:format=type}" />
            <parameter name="@message" layout="${exception:format=message}" />
            <parameter name="@stackTrace" layout="${exception:format=stackTrace}" />
            <parameter name="@innerException" 
                        layout="${exception:format=:innerFormat=ShortType,Message,Method:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}" />
            <parameter name="@additionalInfo" layout="${message}" />
    </target>
</targets>

<rules>
    <!-- database logger -->
    <logger levels="Error,Warn,Fatal" name="databaseLogger" writeTo="database"/>
</rules>

This has also been tested from my .NET application and it logs correctly to the SQL Database.

Now onto JSNLog. I have installed the JSNLog.NLog NuGet package and added <%= JSNLog.JavascriptLogging.Configure() %> to my .ASPX page as directed by the installation instructions.

However if I call JL().info("Testing From JS"); from my ASPX page, or even if I call a function that doesn't exist to produce an error, I do not see the log that should get sent to NLog stored in my SQL Database. JSNLog is definitely installed correctly as I get this added to my Console: enter image description here

So it seems that JSNLog is not passing the log onto NLog? Is there something that I have missed in this installation?


Solution

  • So today is one of those days as a developer where you feel humbled by your own stupidity. It turns out that the hidden answer to my question was simply that you have to make sure you call JSNLog with the exact same name as the NLog logger you have created in your NLog.config.

    For example, my NLog logger was called databaseLogger

    <rules>
        <!-- database logger -->
        <logger levels="Error,Warn,Fatal" name="databaseLogger" writeTo="database"/>
    </rules>
    

    So when I log from JSNLog I need to make sure I call

    JL("databaseLogger").error("This is an error!");