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

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

问题描述

这是个坏主意,我知道,但是...我想在没有配置文件的情况下从头开始以编程方式配置 log4net.我正在为我和我的团队开发一个简单的日志记录应用程序,用于我们负责的一系列相对较小的部门应用程序.我希望他们都登录到同一个数据库.日志记录应用程序只是预配置了 AdoNetAppender 的 log4net 的包装器.

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 部署的,这在部署配置文件时存在一个小问题.如果配置文件是核心项目的一部分,我可以设置它的属性以与程序集一起部署.但它是链接应用程序的一部分,因此我无法选择将它与主应用程序一起部署.(如果这不是真的,请有人告诉我).

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).

可能是因为这是一个坏主意,似乎没有多少示例代码可用于从头开始以编程方式配置 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?

推荐答案

我过去这样做的一种方法是将配置文件作为嵌入式资源包含在内,并且只使用了 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天全站免登陆