Getting JSNLog to log with NLog

2019-08-20 01:18发布

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?

2条回答
手持菜刀,她持情操
2楼-- · 2019-08-20 01:35

I am the author of JSNLog. I saw this same question on the JSNLog github issues page where I wrote an answer.

Because this scenario (logging to 2 logging packages based on severity) is uncommon, I didn't attempt to troubleshoot this.

I can offer these suggestions though:

  • Write a Common.Logging adapter that writes to both NLog and Elmah based on severity, as detailed in my answer on the github issues page.

  • Log everything to NLog, and use an NLog target to send all exceptions on to Elmah. In this scenario you would install JSNLog.NLog to log client side entries to NLog: https://github.com/NLog/NLog.Elmah

  • If you are using Elmah for its built in log viewer, you may want to consider switching to Serilog and Seq. This allows you to send all logs to the one logging package. Seq lets you define filters, so you can create a filter that only shows critical errors.

查看更多
爷的心禁止访问
3楼-- · 2019-08-20 01:44

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!");
查看更多
登录 后发表回答