简体   繁体   English

使用JLog获取JSNLog进行日志记录

[英]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. 我目前正在尝试为我的ASP.Net应用程序实现一个类似于系统的日志记录系统,其中Elmah用于捕获所有发生的.NET Uncaught异常并将它们记录到SQL数据库中,而NLog则用于更轻量的Debug。 ()和Info()调用,这些调用也将记录到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. 除此之外,我还想添加JSNLog,以便可以将所有Javascript Debug / Info / Errors等发送到NLog以便存储在SQL数据库中。

I have installed Elmah via the NuGet Package and made the necessary changes to my web.config 我已经通过NuGet软件包安装了Elmah,并对我的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. 而且我可以确认Elmah正确地登录到我的SQL数据库。

I have also installed the NLog and NLog.Config NuGet packages, along with adding two nodes to the NLog.config file 我还安装了NLogNLog.Config NuGet软件包,以及将两个节点添加到NLog.config文件中

<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. 我的.NET应用程序也对此进行了测试,并将其正确记录到SQL数据库中。

Now onto JSNLog. 现在进入JSNLog。 I have installed the JSNLog.NLog NuGet package and added <%= JSNLog.JavascriptLogging.Configure() %> to my .ASPX page as directed by the installation instructions. 我已经安装了JSNLog.NLog NuGet程序包,并按照安装说明的说明将<%= JSNLog.JavascriptLogging.Configure() %>到了我的.ASPX页面。

However if I call JL().info("Testing From JS"); 但是,如果我调用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. 从我的ASPX页面上,或者即使我调用了一个不存在的函数来产生错误,我也没有看到应该发送到NLog的日志存储在SQL数据库中。 JSNLog is definitely installed correctly as I get this added to my Console: 当我将其添加到控制台时,肯定已正确安装了JSNLog: 在此处输入图片说明

So it seems that JSNLog is not passing the log onto NLog? 因此,似乎JSNLog没有将日志传递到NLog上? Is there something that I have missed in this installation? 在此安装中我缺少什么?

I am the author of JSNLog. 我是JSNLog的作者。 I saw this same question on the JSNLog github issues page where I wrote an answer. 我在JSNLog github问题页面上看到了相同的问题,并在其中写了答案。

Because this scenario (logging to 2 logging packages based on severity) is uncommon, I didn't attempt to troubleshoot this. 因为这种情况(根据严重性记录到2个日志记录包)并不常见,所以我没有尝试解决此问题。

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. 编写一个Common.Logging适配器,该适配器根据严重性同时写入NLog和Elmah,如我在github问题页面上的答案中所述。

  • Log everything to NLog, and use an NLog target to send all exceptions on to Elmah. 将所有内容记录到NLog,并使用NLog目标将所有异常发送到Elmah。 In this scenario you would install JSNLog.NLog to log client side entries to NLog: https://github.com/NLog/NLog.Elmah 在这种情况下,您将安装JSNLog.NLog以将客户端条目记录到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. 如果您使用Elmah的内置日志查看器,则可能要考虑切换到Serilog和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. Seq允许您定义过滤器,因此您可以创建仅显示严重错误的过滤器。

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. 事实证明,对我的问题的隐藏答案仅仅是,您必须确保使用与在NLog.config中创建的NLog记录器完全相同的名称来调用JSNLog。

For example, my NLog logger was called databaseLogger 例如,我的NLog记录器称为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 因此,当我从JSNLog登录时,我需要确保致电

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM