log4net的定制的appender:如何记录,将使用自定义附加目的地写消息? [英] Log4Net custom appender : How to logs messages that will wrote using the Custom appender?
问题描述
问题解决了 - 我编辑这篇文章用正确的代码
我试图写的主函数初始化log4net的记录器+附件到自定义的appender并发送消息认为它 - 这是我尝试(没有成功可惜)
的什么是错我的初始化(Form1中?在下面的.cs)的
命名空间WindowsFormsApplication1
{
公共部分Form1类:表格
{
ILog的日志= LogManager.GetLogger(typeof运算(Form1中));
公共Form1中()
{
log4net.Config.XmlConfigurator.Configure();
的InitializeComponent();
}
私人无效的button1_Click(对象发件人,EventArgs五)
{
log.Info(创建日志);
}
}
错误消息-Exception = {无法加载文件或组装'MessageBoxAppender或它的一个依赖系统找不到指定的文件。:MessageBoxAppender}
[IMG]的 http://i57.tinypic.com/qrjcjc.png [/ IMG]
我试着写从链接这个自定义追加程序代码如下
日志消息
的 http://www.alteridem.net/2008/01/10/writing-an-appender-for-log4net/
我的目标是点击一个按钮和一个日志消息会写想到了自定义的追加程序。
我有3个文件/类
1.Form1.cs Windows窗体 - 只包含一个按钮,应该写一条消息,并初始化
$ b。$ b
2MessageBoxAppender.cs - 自定义的追加,从AppenderSkeleton
3.app.config继承 - 为log4net的配置
的app.config
<?XML版本= 1.0编码=UTF-8>?;
<结构>
< configSections>
<节名称=log4net的
型=log4net.Config.Log4NetConfigurationSectionHandler,log4net的/>
< / configSections>
<&log4net的GT;
<追加程序名称=MessageBoxAppender
型=WindowsFormsApplication1.MessageBoxAppender,WindowsFormsApplication1>
<布局类型=log4net.Layout.PatternLayout>
< ConversionPattern值=%M/>
< /布局>
< /附加器>
<根和GT;
<电平值=ALL/>
<附加目的地-REF REF =MessageBoxAppender/>
< /根>
< / log4net的>
< /结构>
MessageBoxAppender定制的appender
使用log4net.Appender;
使用log4net.Core;
使用系统;
使用System.Collections.Generic;使用System.Diagnostics程序
;
使用System.Linq的;
使用System.Text;使用System.Threading.Tasks
;使用System.Windows.Forms的
;
命名空间WindowsFormsApplication1
{
公共类MessageBoxAppender:AppenderSkeleton
{
///<总结>
///写入日志事件一个MessageBox
///< /总结>
覆盖保护无效追加(LoggingEvent所LoggingEvent所)
{
字符串标题=的String.Format({0} {1},
loggingEvent.Level.DisplayName,
loggingEvent.LoggerName);
字符串消息=的String.Format(
{0} {1} {1} {2} {1} {1}(是继续,没有调试),
RenderLoggingEvent(LoggingEvent所),
Environment.NewLine,
loggingEvent.LocationInformation.FullInfo);
DialogResult的结果= MessageBox.Show(消息,标题,MessageBoxButtons.YesNo);
如果(结果== DialogResult.No)
{
Debugger.Break();
}
}
///<总结>
///此附加器需要与LT;见CREF =布局/>进行设置。
///< /总结>
改写保护布尔RequiresLayout
{
获得{返回true; }
}
}
}
-
我不知道这条线在App.config是正确的 - 得到的回答
<追加程序名称=MessageBoxAppender
型=WindowsFormsApplication1.MessageBoxAppender,MessageBoxAppender>
< /附加器>
时的约定是
键入=命名空间+自定义的追加程序的类名称,自定义追加程序的类名称>
我添加到我的代码:
VAR错误=日志管理。.GetRepository()ConfigurationMessages.Cast< log4net.Util.LogLog>();
要使用的类型属性的值是类的完全合格的名称。这是完整路径类的附加器(命名空间+类名),其次是程序集的名称。它是在你的代码,这将是(假设你的组件名为 WindowsFormsApplication1
- 你可以在你的项目的属性进行检查):
<追加程序名称=MessageBoxAppender
型=WindowsFormsApplication1.MessageBoxAppender,WindowsFormsApplication1>
< /附加目的地>
Issue was solved - I edit this post with the right code.
I am trying to wrote the "main" function that initialize the log4net logger + attachment to the Custom appender and send message thought it - this is my try (without success Unfortunately)
What is wrong with my initialize (Form1.cs below)?
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
ILog log = LogManager.GetLogger(typeof(Form1));
public Form1()
{
log4net.Config.XmlConfigurator.Configure();
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
log.Info("Creating log");
}
}
Error message -Exception = {"Could not load file or assembly 'MessageBoxAppender' or one of its dependencies. The system cannot find the file specified.":"MessageBoxAppender"} [IMG]http://i57.tinypic.com/qrjcjc.png[/IMG]
I try to write logging messages with this custom appender code from the link below
http://www.alteridem.net/2008/01/10/writing-an-appender-for-log4net/
My goal is to click on a button and a log message will write thought the custom appender.
I have 3 files/class.
1.Form1.cs windows form – contain only a button that should write a message and the initialize.
2."MessageBoxAppender.cs" - the custom appended that inherit from "AppenderSkeleton"
3.app.config - for the log4net configuration
app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="MessageBoxAppender"
type="WindowsFormsApplication1.MessageBoxAppender, WindowsFormsApplication1">
<layout type="log4net.Layout.PatternLayout">
<ConversionPattern value="%m" />
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="MessageBoxAppender" />
</root>
</log4net>
</configuration>
MessageBoxAppender custom appender
using log4net.Appender;
using log4net.Core;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class MessageBoxAppender : AppenderSkeleton
{
/// <summary>
/// Writes the logging event to a MessageBox
/// </summary>
override protected void Append(LoggingEvent loggingEvent)
{
string title = string.Format("{0} {1}",
loggingEvent.Level.DisplayName,
loggingEvent.LoggerName);
string message = string.Format(
"{0}{1}{1}{2}{1}{1}(Yes to continue, No to debug)",
RenderLoggingEvent(loggingEvent),
Environment.NewLine,
loggingEvent.LocationInformation.FullInfo);
DialogResult result = MessageBox.Show(message, title, MessageBoxButtons.YesNo);
if (result == DialogResult.No)
{
Debugger.Break();
}
}
/// <summary>
/// This appender requires a <see cref="Layout"/> to be set.
/// </summary>
override protected bool RequiresLayout
{
get { return true; }
}
}
}
I am not sure that this line in the app.config is correct - was answered
<appender name="MessageBoxAppender" type="WindowsFormsApplication1.MessageBoxAppender, MessageBoxAppender"> </appender>
Is the convention is
type="namespace + custom appender class name, custom appender class name>
[Edit] i add to my code:
var errors = LogManager.GetRepository().ConfigurationMessages.Cast<log4net.Util.LogLog>();
The value to use for the type attribute is a fully-qualified name of the class. This is the full path to the class for the appender (namespace + class name), followed by the name of the assembly that it is in. For your code, this will be (assuming that your assembly is called WindowsFormsApplication1
- you can check this in the properties of your project):
<appender name="MessageBoxAppender"
type="WindowsFormsApplication1.MessageBoxAppender, WindowsFormsApplication1">
</appender>
这篇关于log4net的定制的appender:如何记录,将使用自定义附加目的地写消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!