You’re most probably here because you have already read my article How to watch your log through your application in Log4Net and you’re thinking “that’s great, but I need to somehow differentiate all of this output!” – either that or you just really need a handy way to format certain keywords within your RichTextBox.

Either way, here’s my solution. First of all define your logging levels (or keywords) within your RichTextBox container:

Dictionary levels = new Dictionary();

should be defined somewhere within your Form class, probably at the top. This will create a container of key strings that will be your keywords, associated to particular colours that you want these keywords to be coloured within your RichTextBox. Then you need to populate it within your constructor:

// Add colours to correspond to given log levels
levels.Add("INFO", Color.Green);
levels.Add("WARN", Color.Yellow);
levels.Add("ERROR", Color.Red);
levels.Add("DEBUG", Color.Blue);

Next you need to write a function that will iterate through each of these levels/keywords, and search for each instance within your text. Once you’ve found each one you just need to select the word and set the selection colour to the colour that corresponds with that keyword:

private RichTextBox FormatLogColours(RichTextBox textbox) {
// Iterate through each level
foreach (var level in levels) {
int pos = 0; // And look for every instance of it within our text
while ((pos = textbox.Text.IndexOf(level.Key, pos)) > 0) {
// Then select the text by getting the position and selecting from the position for the length of said level
textbox.Select(pos, level.Key.Length); // And then set the corresponding colour
textbox.SelectionColor = level.Value; pos++;
}
} return textbox;
}

Once you have this function defined you can essentially use it anywhere relevant. In the case of our previous article this would be write after our RichTextBox is updated by our LogWatcher event:

LogTextbox = FormatLogColours(LogTextbox);

Nice and simple! I hope this helps someone, even if it’s somewhere to start – you could easily expand it to cover all of the remaining levels, the ones used here are just the ones I used myself but there are many more or you could even expand it to the enter line by looking for the \r\n prior and after the current keyword. If you think of anything good please feel free to add a link in the comments below!

Colour your Log4Net events in your RichTextBox zz的更多相关文章

  1. log4net封装类 zz

    封装说明: 1.使用静态方法调用即可写入日志. 2.在日志信息写入之前,定义委托处理日志信息,便于记录日志信息之前,显示给用户. 3.添加代码配置Log4net,避免应用程序总是携带配置文件.如果需要 ...

  2. Linux服务器init 5启动图形界面,报错Retrigger failed udev events的解决方法

    1.开启linux系统的桌面环境,使用startx未成功,报如下错误: 提示:Retrigger failed udev events [root@ /]# startx xauth: creatin ...

  3. log4net RemotingAppender 的配置

    Before you even start trying any of the alternatives provided, ask yourself whether you really need ...

  4. log4net的各种Appender配置示例

    Apache log4net™ Config Examples Overview This document presents example configurations for the built ...

  5. Log4net 日志使用介绍

    概述 Log4net 有三个主要组件:loggers,appenders 和 layouts.这三个组件一起工作使得开发者能够根据信息类型和等级(Level)记录信息,以及在运行时控制信息的格式化和信 ...

  6. log4net 发布到生产环境不写日志的解决方法--使用 NLog日志

    1.升级到log4net的最新版 PM下执行 Install-Package log4net 还是无法解决的,使用下面的方法 2.使用Nlog替换之,详见https://github.com/NLog ...

  7. log4net Tutorial

    Introduction One of the greatest logging tools out there for .NET is log4net. This software is the g ...

  8. How to do logging in C# with log4net

    If you are writing server code in C# or to a lesser extent desktop/client then it's a good idea to i ...

  9. Log4Net 手册

    首先感慨下,现在的程序员做的工作因为高级语言的生产力,系统框架模式的成熟,开源大牛的贡献,已经成越来越偏向 “面向配置编程”了...... 详细使用指南见文章:http://blog.csdn.net ...

随机推荐

  1. javascript 基础3第13节

    <html> <head> <title>javascript基础</title> </head> <body> 1.流程控制 ...

  2. 01_JavaMail_01_邮件服务器简述

    [收发邮件简单流程] 过程大致是: 发邮件时从客户端发邮件发送到邮件服务器,收邮件就是把邮件服务器的邮件下载到客户端. [邮件协议] * SMTP:(Simple Mail Transfer Prot ...

  3. git ignore已经checked in files

    对于untracked file, 可以使用.gitignore 对于已经checked in file,可以使用git update-index #隐藏 git update-index --ass ...

  4. 【原创】Linux编译内核

    Linux 编译内核 大致分这几个步骤 1.准备编译工具.内核文件 2.开始编译 3.将编译好的新内核install进系统 4.查看是否成功.   一.准备好必备的库和内核文件   1.添加必备的编译 ...

  5. js 中用Dom2级事件处理函数(改变样式)

    下面这些客户端 javascript代码用到了事件,它给一个很重要的事件--“load" 事件注册了一个事件处理程序.同时展示了注册”click“事件处理函数更高级的一种方法 <!do ...

  6. properties文件的继承(套用)关系

    现项目中有多个配置文件分布于/props____def.properties____/env_______def.propertiess_______/dev_______def.properties ...

  7. SimpleXML解析xml文件

    SimpleXML 扩展提供了一种获取 XML 元素的名称和文本的简单方式. 与 DOM 或 Expat 解析器相比,SimpleXML 仅仅用几行代码就可以从 XML 元素中读取文本数据. Simp ...

  8. 在Windows下不使用密码远程登陆Linux

    在登陆Linux进行管理的时候我们通常会使用用户名和密码进行登陆,这样一来是比较麻烦,二来是不安全,为了解决这个问题,我们可以使用公私钥 (public keys和private keys)进行认证. ...

  9. 也说_T、_TEXT、TEXT、L

    本片文章转载自:http://www.cnblogs.com/sobe/archive/2011/03/14/1984188.html 百度或谷歌一下,有很多大牛早已经写过无数相关的文章说明这几个宏的 ...

  10. IOS多线程之NSThread

    参考:http://blog.csdn.net/totogo2010/article/details/8010231 1 简介 NSThread: 优点:NSThread 比其他两个轻量级 缺点:需要 ...