NLOG - 写入空到可选的数据库列 [英] NLog - Write NULL to optional database column

查看:425
本文介绍了NLOG - 写入空到可选的数据库列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NLOG 用于记录在ASP.Net应用程序,并利用该数据库的目标与微软的SQL Server。

I am using NLog for logging in an ASP.Net application and making use of the database target with Microsoft Sql Server.

我有一些记录参数是并不能总是规定。我想,这些被写成不提供时,他们不过 NLOG 似乎总是写他们作为空字符串。

I have some logging parameters that are optional and not always specified. I would like these to be written as null when they are not provided, however NLog seems to always write them as empty strings.

有没有一种方法来配置它写null作为默认?

Is there a way to configure it to write null as the default?

编号: <一个href="http://nlog-project.org/wiki/Database_target">http://nlog-project.org/wiki/Database_target

推荐答案

也许比我提出以下是使用INSERT语句使用数据库的程序登录到改变比较明显的解决方案。如果您使用的数据库程序,那么你可以处理来自为null自己空字符串的交叉混合。我不知道为确保您可以使用数据库程序用NLOG的数据库对象。 log4net的支持它,所以我的猜测是,NLOG确实也是如此。

Maybe a more obvious solution than what I propose below would be to change from using an INSERT statement to using a database procedure to log. If you use a database procedure then you could handle the swizzling from empty string to null yourself. I don't know for sure that you can use a database procedure with NLog's Database target. Log4net supports it so my guess is that NLog does as well.

下面是一个例子(在答案的链接的问题),我发现的配置从别人使用NLOG登录到使用存储过程的数据库。

Here is one example (in the answer to the linked question) I found of the configuration from someone using NLog to log to a database using stored procedure.

<一个href="http://nlog-forum.1685105.n2.nabble.com/Using-a-stored-procedure-for-the-DB-Target-td2621725.html" rel="nofollow">http://nlog-forum.1685105.n2.nabble.com/Using-a-stored-procedure-for-the-DB-Target-td2621725.html

我在这里看到:

HTTP://nlog.$c$cplex.com/workitem/5418

一个抱怨它不工作(至少在NLOG 2.0测试版)。

A complaint that it doesn't work (at least in the NLog 2.0 beta).

的两个例子之间的一个区别是,工作例使用EXEC LoggingProcedureName ...,而非工作人使用LoggingProcedureName ...

One difference between the two examples is that the working example uses "exec LoggingProcedureName ..." while the nonworking one uses "LoggingProcedureName ..."

希望这有助于。

我不能,为什么NLOG写emptry字符串,而不是空或如何使NLOG写零,而不是空字符串的评论,但我不知道你能不能让他们你想要的方式这项工作通过额外的配置?

I can't comment on why NLog writes emptry strings rather than null or how to make NLog write null instead of empty strings, but I wonder if you could make this work they way you want through additional configuration?

在有记录参数可选?是否有某些地方在code,你一直记录的一些价值观和其他地方,你千万不要登录一些值?你能(为开发者)知道哪些可选参数,适用于应用程序的哪些部分?

When are the logging parameters optional? Are there certain places in your code where you ALWAYS log some values and other places where you NEVER log some values? Can you (as the developer) know which optional parameters are applicable in which sections of your application?

您可以配置多个数据库目标,每个正确的参数中指定?然后,您可以点记录器的特定数据库的目标是适合于code的位置。

Could you configure multiple Database Targets, each with the "right" parameters specified? You could then point Loggers to the specific Database Target that is appropriate for the code location.

假设你的应用程序被分成(由命名空间)到$ c表示(通常情况下)执行前,十一五期间$ C,和之后。

Say that your application is divided (by namespace) into code that (generally) executes "before", "during", and "after".

在code之前,你可能会被记录参数答:在十一五期间code,你可能会被记录参数B.在后code,你可能会测井参数C.所以,你的记录表可能有这样的列:

In the "before" code, you might be logging parameter A. In the "during" code, you might be logging parameter B. In the "after" code, you might be logging parameter C. So, your logging table might have columns like:

DateTime, Logger, LogLevel, A, B, C, Message, Exception

现在你有一个数据库的目标是将所有这些值的每一个记录的语句。

Right now you have a Database Target that inserts all of those values for every logging statement.

如果你有三个数据库的目标是插入值,像这样:

What if you have three Database Targets that insert the values like so:

DataTime, Logger, LogLevel, A, Message, Exception
DataTime, Logger, LogLevel, B, Message, Exception
DataTime, Logger, LogLevel, C, Message, Exception

您可以配置部分是这样的:

You could configure your section something like this:

  <rules>
    <logger name="Before.*" minlevel="Trace" writeTo="databaseA" />
    <logger name="During.*" minlevel="Trace" writeTo="databaseB" />
    <logger name="After.*" minlevel="Trace" writeTo="databaseC" />
  </rules>

显然,可能有几个问题这样的想法:

Obviously, there could be several problems with this idea:

  1. 这是可能的(或容易)分配你的记录器以匹配参数的可选性。

  1. It might be possible (or easy) to divide your loggers up to match the "optionality" of the parameters.

有可能是可选的参数太多,组合,使这个可行的(可能是相同的缺点:1)。

There might be too many combinations of optional parameters to make this feasible (probably the same drawback as 1).

这未必是有记录的活动数据库对象在同一时间一个不错的主意。也许这会导致性能问题。

It might not be a good idea to have a log of database targets active at one time. Maybe this would lead to performance issues.

嗯,这是我所有的。我不知道我的想法,甚至会工作,更何况,如果它是可行的。

Well, that's all that I have. I don't know my idea would even work, much less if it is practical.

可能是更好的解决办法是NLOG允许对每个数据库参数一个额外的属性,让你说送,而不是空字符串空。

Probably the better solution would be for NLog to allow an extra property on each database parameter that would allow you to say "send null instead of empty string".

我想我应该建议你可能还需要问这个问题在 NLOG论坛。在该论坛克雷格今天问了同样的(或类似)的问题更早。也许你是克雷格。

I guess I should suggest that you might also want to ask this question in the NLog forum. "Craig" on that forum asked the same (or similar) question earlier today. Maybe you are Craig.

这篇关于NLOG - 写入空到可选的数据库列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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