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. Core Data(数据持久化)

    Core Data可能是OS X和iOS中最容易被误解的框架之一了.为了帮助大家理解,我们将快速研究Core Data,来看一下它是关于什么的.为了正确使用Core Data, 有必要理解其概念.几乎 ...

  2. hpple 简单使用

    最近项目使用到hpple,简单说一下使用方式,做做笔记 let responseData = response as! NSData let utf8Html = responseData.strin ...

  3. Emgu CV的一个异常的解决方法

    今年组里有大项目落我头上了,并不能像去年一样回家还能搞搞Cocos2dX,一把老泪流了下来... 回到正题,由于组里需要做一个显示板的自动测试项目,涉及到Computer Vision.不得不说,这才 ...

  4. 06_WebService与Socket的区别

    [区别] 区别1. Socket是基于TCP/IP的传输层协议. WebService是基于HTTP协议传输数据的,HTTP是基于TCP的应用层协议. 区别2. WebService采用了基于HTTP ...

  5. ZEngine游戏框架需求稿

    早在2013就开始接触cocos2dx游戏框架,觉得的使用框架对于游戏开发者来说有些不方便.我便想在cocos2dx的框架基础之上实现ZEngine库,方便开发者编辑动画,以及着重于对游戏逻辑的处理. ...

  6. 使用自定义 jQuery 插件的一个选项卡Demo

    前几天闲着没事,想着编写一个 jQuery 插件,或许这将是一个美好的开始. 这里是html页面: <!DOCTYPE html> <html lang="en" ...

  7. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

    http://quote.eastmoney.com/center/list.html#28003501_0_2 http://bbs.tianya.cn/post-53726-21098-1.sht ...

  8. Android中图片的异步加载

    转: 1.  为什么要异步加载图片 下载图片比较费时,先显示文字部分,让加载图片的过程在后台,以提升用户体验 2.  SoftReference的作用 栈内存—引用 堆内存—对象 Eg: Object ...

  9. H5小内容(二)

    音视频处理   视频处理     基本内容       使用Flash技术处理HTML页面中的视频内容         包含音频.动画.网页游戏等  特点    浏览器原生不支持(IE浏览器要求安装A ...

  10. aspx页面中, <%= % > 与 <%# % > 的区别

    关于这个问题,在多数的 ASP.NET 的教材中,都提到了一些. <%= % >与 <%# % >的区别在于:绑定时机不同, <%# % >是在控件调用DataBi ...