Getting JSNLog to log with NLog

谁都会走 提交于 2019-12-11 16:42:09

问题


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:

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


回答1:


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.




回答2:


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!");


来源:https://stackoverflow.com/questions/46485108/getting-jsnlog-to-log-with-nlog

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