Colour your Log4Net events in your RichTextBox zz
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的更多相关文章
- log4net封装类 zz
封装说明: 1.使用静态方法调用即可写入日志. 2.在日志信息写入之前,定义委托处理日志信息,便于记录日志信息之前,显示给用户. 3.添加代码配置Log4net,避免应用程序总是携带配置文件.如果需要 ...
- Linux服务器init 5启动图形界面,报错Retrigger failed udev events的解决方法
1.开启linux系统的桌面环境,使用startx未成功,报如下错误: 提示:Retrigger failed udev events [root@ /]# startx xauth: creatin ...
- log4net RemotingAppender 的配置
Before you even start trying any of the alternatives provided, ask yourself whether you really need ...
- log4net的各种Appender配置示例
Apache log4net™ Config Examples Overview This document presents example configurations for the built ...
- Log4net 日志使用介绍
概述 Log4net 有三个主要组件:loggers,appenders 和 layouts.这三个组件一起工作使得开发者能够根据信息类型和等级(Level)记录信息,以及在运行时控制信息的格式化和信 ...
- log4net 发布到生产环境不写日志的解决方法--使用 NLog日志
1.升级到log4net的最新版 PM下执行 Install-Package log4net 还是无法解决的,使用下面的方法 2.使用Nlog替换之,详见https://github.com/NLog ...
- log4net Tutorial
Introduction One of the greatest logging tools out there for .NET is log4net. This software is the g ...
- 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 ...
- Log4Net 手册
首先感慨下,现在的程序员做的工作因为高级语言的生产力,系统框架模式的成熟,开源大牛的贡献,已经成越来越偏向 “面向配置编程”了...... 详细使用指南见文章:http://blog.csdn.net ...
随机推荐
- ServiceStack.Redis 破解
在github上下载了ServiceStack.Redis,做测试发现有限制,居然从v4开始就收费,无聊时,做了个源码分析 废话不多,上测试代码 try { ; i < ; i++) { red ...
- 将表单数据转化为json数据
/** * 将Form表单转成符合后台要求的json格式数据 * @param frm form表单Id * * @return json格式的数据 */function getFormJson(fr ...
- 对REST的一些理解
昨天学习REST,发现有篇文章写的真心不错,看了一遍,并没有完全理解,将一些感觉比较重要的做个记录. 文章链接:REST简介 定义 Representational State Transfer ( ...
- 九度OJ 1453 Greedy Tino -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1453 题目描述: Tino wrote a long long story. BUT! in Chinese... ...
- 通过telnet使用smtp协议发送邮件
smtp协议是一个简单的邮件传输协议,利用它我们可以将邮件发送给别人,这里将通过telnet这个程序利用smtp协议从网易向gmail发送一封邮件 网上不少有说使用telnet发送邮件的文章,我也看过 ...
- Java中静态代码块,代码块,构造方法的理解
直接贴代码 class A { static { System.out.println("父类静态代码区"); } { System.out.println("父类代码区 ...
- .NET异步操作学习之一:Async/Await中异常的处理
以前的异常处理,习惯了过程式的把出现的异常全部捕捉一遍,然后再进行处理.Async/Await关键字出来之后的确简化了异步编程,但也带来了一些问题.接下来自己将对这对关键字进行学习.然后把研究结果放在 ...
- 数据库(MSSQLServer,Oracle,DB2,MySql)常见语句以及问题(续1之拼接字符串)
上一篇文章http://www.cnblogs.com/valiant1882331/p/4056403.html写的太长了,所以就换了一篇,链接上一节继续 字符串的拼接 MySql中可以使用&quo ...
- mac 生成支付宝的rsa公钥和私钥 php版本
openssl genrsa -out rsa_private_key.pem 1024 公钥 openssl rsa -in rsa_private_key.pem -pubout -out rsa ...
- 大量字段表单在PHP便捷处理分享
关于程序开发中的表单批量提交策略很多时候一个表单太多的字段,如何能够高效获取表单字段,也为如何提神开发的效率和统一性? 比如一个系统的某个有26个字段,那么我用表单的名称用26个a到z的字母, 你是选 ...