大约5月前 - 3个评论
log4net是Apache开发的一个用于.NET应用程序的logger工具,log4net是开源的,使用Apache License, Version 2.0开源协议。 可以到这里下载log4net: http://logging.apache.org/log4net/download.html 下面来具体介绍下如何使用log4net: 1. 将log4net.dll这个assembly加到项目的引用中。 2. 初始化configuration file, 指定要使用的configuration类型 ,支持的类型有:XmlConfigurator 和 DOMConfigurator 例如:初始化一个XmlConfigurator并指定当前项目的config文件为provider: log4net.Config.XmlConfigurator.Configure(); XmlConfigurator.Configure()这个方法有很多个重载,可以在重载方法的参数中指定config文件的路径。 如果是Web Applciation,可以在Global.asax的Application_Start方法中作这个初始化,这样整个Web Application就不需要在每次使用log4net的时候都初始化configuration了。 例如,我的Global.asax文件中Application_Start内容是这样的: protected void Application_Start(object sender, EventArgs e) { //The Log4net component needs initial configure at the start of the application. log4net.Config.XmlConfigurator.Configure(); } 3. 修改项目的config文件: a) 把<section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler, log4net” />加入到<configSections>与</configSections>之间。 b) 添加下面的示例到<configuration>与</configuration>中: <log4net>
大约5月前 - 5个评论
C#的internal关键字可以使标记的方法,字段或者属性等等只能在当前assembly内部使用,那么如果其他的assembly需要使用这个internal的方法的时候怎么办呢?.NET提供了一种类似于C++中的友元类的方式来完成这个功能,那就是使用InternalsVisibleTo。 这种情况常见于做测试的时候,需要另外一个项目来测试项目中的internal方法所标记的功能,所以有了InternalsVisibleTo,我们就不用为了做单元测试而把一个本不该公开的方法改为public了. 使用InternalsVisibleTo还有一些需要注意的地方,特别是PublicKey不太容易弄的明白,下面先来说说这个InternalsVisibleTo该怎么使用: 先来说明一下前提:Project1是功能项目,Project1.Test (assembly name: Project1.Test.dll)是为做Project1的测试工程。 1. 打开Project1的Assembly.cs文件,在文件的末尾加上这样一句话: [assembly: InternalsVisibleTo("Project1.Test, PublicKey=******")] 其中PublicKey=******应该替换成Project1.Test.dll的public key,至于如何获取PublicKey,请看文章末尾的Notes部分. 2. 确认namespace: System.Runtime.CompilerServices 添加到了Assembly.cs的namespace引用中,因为InternalsVisibleTo位于命名空间System.Runtime.CompilerService中。 Notes: 1. 如何获取PublicKey? A: 在命令行下,使用sn -Tp Project1.Test.dll就可以看到PublicKey和PublicKeyToken 2. 如果Project1是个strong-named的项目,那么InternalsVisibleTo必须指定PublicKey,所以Project1.Test也必须使用强签名才能正确使用InternalsVisibleTo, 不然编译会出错,如果Project1没有使用强签名,那么Project1.Test也不必使用强签名,而且在使用InternalsVisibleTo的时候只需要程序集的名字就可以了,不需要设置PuklicKey。 相关文章:DotNet下NUnit的使用(2)—- 第一个NUnit工程 性能: 字符串倒序算法 (C# version) 在.NET项目中使用log4net DotNet下NUnit的使用(1)—- NUnit入门 Update the application configuration file automatically in C# How to use Sql Azure 让Linux走出虚拟机 WordPress插件StyleBox JQuery Ajax发送GET请求乱码的解决办法
大约7月前 - 没有评论
Description: Sql Azure is the cloud computing service provided by Microsoft, which is @CTP version now. Sql Azure will provide you databases on the cloud level, this means you can host your data on the MS’s database server, definitely, it will be more safety. Apply for an Sql Azure account: Even Sql Azure is @CTP,
大约7月前 - 没有评论
C#中有个string类型,是个很特殊的reference type, 存储在内存中一个特殊的“静态池”中,这里是MSDN关于string的介绍:http://msdn.microsoft.com/zh-cn/library/362314fe(VS.80).aspx 由于string本身是一个不可变的字符串的集合,每次对string对象的修改都是新创建一个string对象,string有一个indexer用来访问字符串集合中的每个字符串(string str=”test test”; Console.WriteLine(str[0]);),虽然我们可以使用indexer来访问这个内部维护的字符串集合,但是却不能直接改变他们的值; 所以对字符串逆序,大致有以下几个思路: 先把string转换为char[],然后对char[] 逆序,最后再把逆序后的char[]转化为string。 倒序遍历string,然后用字符拼接的方式组成逆序后的字符串。 先来说第一种: 第一步,把string转化为char[]: [方案一]新建一个char[],用foreach循环对数组进行赋值: string str = "This is just a temporary string"; char[] data = new char[str.Length]; for(int i = 0; i < str.Length; i++) { data[i] = str[i]; } [方案二]使用string.ToCharArray() string str = "This is just a temporary string"; char[] data = str.ToCharArray();
大约1年前 - 没有评论
The structure of an application’s configuration file always include: The root node: start with and end with Configuation sections, connectionStrings, appSettings, custom defined sections, etc. Of course, we can update the configuration sections mannually, the values defined in the configuration file are always stable and seems will never be changed, but we may still need to
大约1年前 - 没有评论
DotNet下NUnit的使用(2)—- 第一个NUnit工程 收藏 准备工作 1. 打开visual studio, 新建一个“Class Library”工程,命名为“NUnitDemo” 2. 在工程中新建一个Class:“FirstNUnitClass”, 在类中添加一个public的方法:GetBonous(int level), 最终类的内容: 准备工作 1. 打开visual studio, 新建一个“Class Library”工程,命名为“NUnitDemo” 2. 在工程中新建一个Class:“FirstNUnitClass”, 在类中添加一个public的方法:GetBonous(int level), 最终类的内容: public int GetBonous(int level) { int bonous = 0; switch (level) { case 1: bonous = 1000; break; case 2: bonous = 2000; break; case 3: bonous = 3000; break;