使用JLog获取JSNLog进行日志记录 [英] Getting JSNLog to log with NLog

查看:112
本文介绍了使用JLog获取JSNLog进行日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试为我的ASP.Net应用程序实现一个类似于 JSNLog ,以便将所有Javascript Debug/Info/Errors等发送到NLog.也可以存储在SQL数据库中.

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.

我已经通过NuGet软件包安装了Elmah,并对我的web.config进行了必要的更改

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>

我可以确认Elmah正确地登录到我的SQL数据库.

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

我还安装了NLogNLog.Config NuGet软件包,并在NLog.config文件中添加了两个节点

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>

这也已经在我的.NET应用程序中经过测试,并且可以正确地记录到SQL数据库中.

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

现在进入JSNLog.我已经安装了JSNLog.NLog NuGet软件包,并按照安装说明的说明将<%= JSNLog.JavascriptLogging.Configure() %>添加到了我的.ASPX页面.

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.

但是,如果我从ASPX页面调用JL().info("Testing From JS");,或者即使我调用了一个不存在的函数也不会产生错误,我也看不到应发送到NLog的日志,该日志存储在SQL数据库中.当我将其添加到控制台时,肯定已正确安装了JSNLog:

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:

因此,似乎JSNLog没有将日志传递到NLog上?在此安装过程中有什么我想念的吗?

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

推荐答案

因此,今天是作为开发人员的那一天,您会因为自己的愚蠢而感到谦卑.事实证明,对我的问题的隐藏答案仅仅是,您必须确保使用与在NLog.config中创建的NLog记录器完全相同的名称来调用JSNLog.

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记录器称为databaseLogger

For example, my NLog logger was called databaseLogger

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

因此,当我从JSNLog登录时,我需要确保致电给我

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

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

这篇关于使用JLog获取JSNLog进行日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆