简体   繁体   English

使用ORM进行日志记录(log4net和OpenAccess)

[英]Logging using ORM (log4net and OpenAccess)

Based previous experience and research, I implemented logging at the workplace using log4Net. 根据以前的经验和研究,我使用log4Net在工作场所实现了日志记录。

However, there was general preference to use a custom logger when log4net was presented in our development meetings. 但是,在我们的开发会议上介绍log4net时,通常倾向于使用自定义记录器。 The reason was in order to use the generated code from Telerik's ORM instead of specifying the insert query in the configuration file. 原因是为了使用从Telerik的ORM生成的代码,而不是在配置文件中指定插入查询。

<appender name="ADONetAppender" type="log4net.Appender.AdoNetAppender">
   <bufferSize value="1"/>
   <connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" />
   <connectionStringName value="Test"/>
   <commandText value="INSERT INTO Log (UniqueId,Date,UserId,Thread,level,Logger,Message,Exception)     VALUES (UUID(),?log_date,?userid,?thread,?log_level,?logger,?message,?exception)" />
   ...

What is your advice? 你有什么建议? Can we create a custom appender for this purpose? 我们可以为此创建自定义追加程序吗? Should we go for the custom logger? 我们应该去定制记录器吗?

For information, there are positive and negative views on custom loggers here . 有关信息,请参见此处对自定义记录器的正面和负面看法。

Thanks 谢谢

You can create a custom logger, and it can use any mechanism you like to log stuff. 您可以创建一个自定义记录器,它可以使用您喜欢的任何机制来记录内容。 Personally I found the AdoNetAppender perfectly performant. 我个人发现AdoNetAppender的性能出色。

However having worked supporting enterprise operations I'd strongly recommend that you use the lowest impedance technique to log your messages. 但是,在支持企业运营的过程中,我强烈建议您使用最低阻抗技术来记录您的消息。 Logs come into their own with debugging production systems. 日志与调试生产系统结合在一起。 I'd avoid writing an appender for the sake of 'db access pattern consistency'. 为了避免“数据库访问模式一致性”,我将避免编写追加程序。

If you are having db, ORM, OS or networking problems the last thing you need is for your logger not to be working. 如果您遇到数据库,ORM,操作系统或网络问题,那么您需要做的最后一件事是使记录器不工作。 File, Windows Debug messges and ETW logging is what I recommend in the first instance and this can be backed up with DB logging and to make it safer I'd put the db logging into at least a different db - so not to affect your production systems. 我首先建议使用文件,Windows调试消息和ETW日志记录,并且可以使用DB日志记录来备份它,并且为了使其更加安全,我将db日志记录至少放入了另一个不同的db中-以免影响您的生产系统。 Preferably it would be on local server where network connectivity won't come into play. 最好是在本地服务器上,其中网络连接不会起作用。

Edit : 编辑

If you really want to you can derive a class from AdoNetAppender and then override the CommandText Property to be custom. 如果确实需要,可以从AdoNetAppender派生一个类,然后重写CommandText属性以进行自定义。 There is an example on here that I can't seem to place, that does this with connectionstring. 这里有一个我似乎无法放置的示例,该示例使用connectionstring进行此操作。

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

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