如何配置log4net的编程从头开始(不配置) [英] How to configure log4net programmatically from scratch (no config)

查看:161
本文介绍了如何配置log4net的编程从头开始(不配置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个坏主意,我知道,但... 我想以编程方式配置log4net的从头开始,没有配置文件。我工作的一个简单的记录应用程序对我和我的团队用了一堆比较小的部门级应用,我们有责任。我希望他们所有的日志到同一个数据库。该记录应用程序仅仅是一个log4net的周围用AdoNetAppender preconfigured包装。

This is a Bad Idea, I know, but... I want to configure log4net programmatically from scratch with no config file. I'm working on a simple logging application for me and my team to use for a bunch of relatively small departmental applications we're responsible for. I want them to all log to the same database. The logging application is just a wrapper around log4net with the AdoNetAppender preconfigured.

所有的应用都部署的ClickOnce,其中presents一个小问题,部署配置文件。如果配置文件为核心项目的一部分,我可以设置其属性部署与装配。但它是一个连接的应用程序的一部分,所以我没有与主应用程序部署它的选项。 (如果这不是真的,有人请让我知道)。

All of the applications are ClickOnce deployed, which presents a small problem with deploying the config file. If the config file were part of the core project, I could set its properties to deploy with the assembly. But it's part of a linked application, so I don't have the option of deploying it with the main application. (If that's not true, somebody please let me know).

可能是因为它是一个坏主意,但似乎并没有太大的样品code可通过编程从头configruating log4net的。这是我到目前为止所。

Probably because it's a Bad Idea, there doesn't seem to be much sample code available for programmatically configruating log4net from scratch. Here's what I have so far.

Dim apndr As New AdoNetAppender()
apndr.CommandText = "INSERT INTO LOG_ENTRY (LOG_DTM, LOG_LEVEL, LOGGER, MESSAGE, PROGRAM, USER_ID, MACHINE, EXCEPTION) VALUES (@log_date, @log_level, @logger, @message, @program, @user, @machine, @exception)"
apndr.ConnectionString = connectionString
apndr.ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
apndr.CommandType = CommandType.Text
Dim logDate As New AdoNetAppenderParameter()
logDate.ParameterName = "@log_date"
logDate.DbType = DbType.DateTime
logDate.Layout = New RawTimeStampLayout()
apndr.AddParameter(logDate)
Dim logLevel As New AdoNetAppenderParameter()
logLevel.ParameterName = "@log_level"
'And so forth...

apndr 配置所有参数后,我的第一次尝试这个...

After configuring all the parameters for apndr, I at first tried this...

Dim hier As Hierarchy = DirectCast(LogManager.GetRepository(), Hierarchy)
hier.Root.AddAppender(apndr)

它没有工作。然后,在黑暗中了一枪,我想这不是。

It didn't work. Then, as a shot in the dark, I tried this instead.

BasicConfigurator.Configure(apndr)

这也不能工作。没有任何人有关于如何编程方式配置log4net的从头开始,没有配置文件中的任何有益的参考?

That didn't work either. Does anybody have any good references on how to configure log4net programmatically from scratch with no config file?

推荐答案

我已经做到了这一点的方法之一是包括配置文件作为嵌入资源,并且只需使用<一href="http://logging.apache.org/log4net/release/sdk/log4net.Config.XmlConfigurator.Configure%5Foverload%5F7.html">log4net.Config.Configure(Stream).

One way I've done this in the past is to include the configuration file as an embedded resource, and just used log4net.Config.Configure(Stream).

这样的话,我可以使用配置语法我是熟悉的,而不必担心会部署一个文件。

That way, I could use the configuration syntax I was familiar with, and didn't have to worry about getting a file deployed.

这篇关于如何配置log4net的编程从头开始(不配置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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