??xml version="1.0" encoding="utf-8" standalone="yes"?>国产精品亚洲AV三区,色播亚洲视频在线观看,亚洲精品国产综合久久一线http://www.tkk7.com/kxx129/category/29519.htmljava学习,心情日记,~纷时刻zh-cnTue, 13 Dec 2016 19:38:43 GMTTue, 13 Dec 2016 19:38:43 GMT60C# 多维数组学习, 使用(2)http://www.tkk7.com/kxx129/archive/2009/03/19/260796.htmlC在线C在线Thu, 19 Mar 2009 07:35:00 GMThttp://www.tkk7.com/kxx129/archive/2009/03/19/260796.htmlhttp://www.tkk7.com/kxx129/comments/260796.htmlhttp://www.tkk7.com/kxx129/archive/2009/03/19/260796.html#Feedback0http://www.tkk7.com/kxx129/comments/commentRss/260796.htmlhttp://www.tkk7.com/kxx129/services/trackbacks/260796.html
GetUpperBound(0) q回数组的第一l的索引上限QGetUpperBound(i)q回数组的i+1l的上限QGetUpperBound(Rank-1)q回数组的最后一l的上限Q也是列数-1
  阅读全文

C在线 2009-03-19 15:35 发表评论
]]>
C# 多维数组学习, 使用http://www.tkk7.com/kxx129/archive/2009/03/18/260601.htmlC在线C在线Wed, 18 Mar 2009 14:58:00 GMThttp://www.tkk7.com/kxx129/archive/2009/03/18/260601.htmlhttp://www.tkk7.com/kxx129/comments/260601.htmlhttp://www.tkk7.com/kxx129/archive/2009/03/18/260601.html#Feedback1http://www.tkk7.com/kxx129/comments/commentRss/260601.htmlhttp://www.tkk7.com/kxx129/services/trackbacks/260601.html
GetUpperBound(0) q回数组的第一l的索引上限QGetUpperBound(i)q回数组的i+1l的上限QGetUpperBound(Rank-1)q回数组的最后一l的上限Q也是列数-1
  阅读全文

C在线 2009-03-18 22:58 发表评论
]]>
C#中处理INI文g的类http://www.tkk7.com/kxx129/archive/2009/03/04/257717.htmlC在线C在线Wed, 04 Mar 2009 02:47:00 GMThttp://www.tkk7.com/kxx129/archive/2009/03/04/257717.htmlhttp://www.tkk7.com/kxx129/comments/257717.htmlhttp://www.tkk7.com/kxx129/archive/2009/03/04/257717.html#Feedback0http://www.tkk7.com/kxx129/comments/commentRss/257717.htmlhttp://www.tkk7.com/kxx129/services/trackbacks/257717.html阅读全文

C在线 2009-03-04 10:47 发表评论
]]>
安装ASP.NET Portal Starter Kit成功完成以后dpȝ出错http://www.tkk7.com/kxx129/archive/2008/05/18/201231.htmlC在线C在线Sun, 18 May 2008 06:16:00 GMThttp://www.tkk7.com/kxx129/archive/2008/05/18/201231.htmlhttp://www.tkk7.com/kxx129/comments/201231.htmlhttp://www.tkk7.com/kxx129/archive/2008/05/18/201231.html#Feedback0http://www.tkk7.com/kxx129/comments/commentRss/201231.htmlhttp://www.tkk7.com/kxx129/services/trackbacks/201231.html非域环境下的安装
问题Q?br /> pȝ提示“用户 'NT AUTHORITY\NETWORK SERVICE' dp|”

解决ҎQ?br />
新徏d
在SQL Server企业理器里,在安全?d?新徏一个登?在名U右边点"..."按钮Q名U选择IIS_WPG,再点成员按钮Q选择NETWORK SERVICE'Q确定以后,使用windowsw䆾验证Q允许访问,数据库选择PortalQ确?/p>

赋予权限
SQL Server企业理?〉安全?〉登?〉双击NT AUTHORITY\NETWORK SERVICE=?#8220;数据库访?#8221;TAB?〉授予对Portal数据库的讉K权限

域环境下的安?br /> 问题Q?br /> pȝ提示“用户 'IUSER_机器? dp|”

解决ҎQ?br /> 操作Ҏ基本相同Q不q不是操作用?'NT AUTHORITY\NETWORK SERVICE' Q而是需要修改IIS的匿名访问用PIUSER_机器名)在SQLServer中的d及权限?/p>
转自:安装ASP.NET Portal Starter Kit成功完成以后dpȝ出错

C在线 2008-05-18 14:16 发表评论
]]>
加密 web.confighttp://www.tkk7.com/kxx129/archive/2008/05/18/201230.htmlC在线C在线Sun, 18 May 2008 06:14:00 GMThttp://www.tkk7.com/kxx129/archive/2008/05/18/201230.htmlhttp://www.tkk7.com/kxx129/comments/201230.htmlhttp://www.tkk7.com/kxx129/archive/2008/05/18/201230.html#Feedback0http://www.tkk7.com/kxx129/comments/commentRss/201230.htmlhttp://www.tkk7.com/kxx129/services/trackbacks/201230.html<%@ Page Language="C#" %>
<%
Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
%>

保存 my.aspx C的web目录 ,q行一下窗体显C?“ NT AUTHORITY\NETWORK SERVICE ”。成功!

2、(关键一步)q行cmd,执行以下

aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY\NETWORK SERVICE"
 

说明Q注册默认的 RsaProtectedConfigurationProvider 的RSA 密钥容器Q?br /> NetFrameworkConfigurationKey ?RsaProtectedConfigurationProvider 的默认provider?成功Q?br />
3、现在,可以加密web.config Q运行:

加密Q?strong>aspnet_regiis -pe "connectionStrings" -app "/Myweb"
说明Q?strong>"connectionStrings" 是要加密的节Q?strong style="color: rgb(32,0,255)">"/Myweb"是的web目录
解密Q?strong>aspnet_regiis -pd "connectionStrings" -app "/Myweb"

成功Q?

4、这样就可以在程序里调用了(不用解密, 哈哈Q:
...
string connstr= ConfigurationManager.ConnectionStrings["myConnstr"].ConnectionString.ToString();
...


同样?也可以用创徏自己的RSA 密钥容器Q如下:
  Q?Q?font size="3">创徏 "MyKeys" 密钥容器Q运行:aspnet_regiis -pc "MyKeys" -exp 
Q?Q、在web.config里加入以下:
<protectedData>
<providers>
<add name="MyProvider"
type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0. 0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d0a3a,
processorArchitecture=MSIL"
keyContainerName="MyKeys"
useMachineContainer="true" />
</providers>
</protectedData>

保存?/span>

Q?Q?/span>授予帐户对计机U别?"MyKeys" RSA 密钥容器的访问权限,q行Q?/font>

aspnet_regiis -pa "MyKeys" "NT AUTHORITY\NETWORK SERVICE"

Q?Q?/span>现在Q可以加密web.config Q运行:

加密Q?/span>aspnet_regiis -pe "connectionStrings" -app "/Myweb" -prov "MyProvider"

说明Q?/span>"connectionStrings" 是要加密的节Q?strong style="color: rgb(32,0,255)">"/Myweb"是的web目录Q?/span>"MyProvider" 自己密钥容器

解密Q?/span>aspnet_regiis -pd "connectionStrings" -app "/Myweb" -prov "MyProvider"

转自:加密 web.config

C在线 2008-05-18 14:14 发表评论
]]>
PetShop4.0安装排错http://www.tkk7.com/kxx129/archive/2008/05/18/201229.htmlC在线C在线Sun, 18 May 2008 06:10:00 GMThttp://www.tkk7.com/kxx129/archive/2008/05/18/201229.htmlhttp://www.tkk7.com/kxx129/comments/201229.htmlhttp://www.tkk7.com/kxx129/archive/2008/05/18/201229.html#Feedback0http://www.tkk7.com/kxx129/comments/commentRss/201229.htmlhttp://www.tkk7.com/kxx129/services/trackbacks/201229.html不知道从什么时候开始,研究asp.net的h都比较推崇petshop。有人研I也有h重构。现?net2.0来了Qpetshop4.0也来了。我今天也装了一个,但是Qؓ了能让这个petshop4q行hQ费了不劲。因?net2.0Ҏ来说太陌生了?br />
“/petshop”应用E序中的服务器错误?br /> --------------------------------------------------------------------------------

配置错误
说明: 在处理向该请求提供服务所需的配|文件时出错。请查下面的特定错误详细信息q当C攚w|文件?

分析器错误信? 未能使用提供E序“RsaProtectedConfigurationProvider”q行解密。提供程序返回错误信息ؓ: 打不开 RSA 密钥容器?/p>

源错?


?2:  <configuration xmlns=" ?3:   <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
?4:    <EncryptedData Type="
?5:     xmlns=" ?6:     <EncryptionMethod Algorithm="  

源文? C:\Program Files\Microsoft\.NET Pet Shop 4.0\Web\web.config    ? 4


--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.42; ASP.NET 版本:2.0.50727.42

解决Ҏ在下面这位兄弟的blog里找C?/p>

http://azhai.cnblogs.com/archive/2006/01/30/324346.html

具体为:

q行cmd,执行以下   aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY\NETWORK SERVICE"    说明Q注册默认的 RsaProtectedConfigurationProvider 的RSA 密钥容器Q?nbsp;   NetFrameworkConfigurationKey ?RsaProtectedConfigurationProvider 的默认provider?成功Q?br />


注意Q你要是同时装了1.1?.0Q请扑ֈ2.0目录下的aspnet_regiis否则当然不成了?br />
然后又碰到第二个问题Q具体描q和解决在下面这个兄弟的blog里?/p>

http://zygoses2gether.cnblogs.com/archive/2006/01/05/311614.html

非域环境下的安装
问题Q?br /> pȝ提示“用户 'NT AUTHORITY\NETWORK SERVICE' dp|”

解决ҎQ?/p>

新徏d
在SQL Server企业理器里,在安全?d?新徏一个登?在名U右边点"..."按钮Q名U选择IIS_WPG,再点成员按钮Q选择NETWORK SERVICE'Q确定以后,使用windowsw䆾验证Q允许访问,数据库选择PortalQ确?/p>

赋予权限
SQL Server企业理?〉安全?〉登?〉双击NT AUTHORITY\NETWORK SERVICE=?#8220;数据库访?#8221;TAB?〉授予对Portal数据库的讉K权限

域环境下的安?br /> 问题Q?br /> pȝ提示“用户 'IUSER_机器? dp|”

解决ҎQ?br /> 操作Ҏ基本相同Q不q不是操作用?'NT AUTHORITY\NETWORK SERVICE' Q而是需要修改IIS的匿名访问用PIUSER_机器名)在SQLServer中的d及权限?br />

l过q个多折腾,l于看见petshop的页面了。哈哈。开始看看代码,以后可能发个研究报告之类的东西出来?/p> 转自:PetShop4.0安装排错

C在线 2008-05-18 14:10 发表评论
]]>
ASP.NET验证码(3U)http://www.tkk7.com/kxx129/archive/2008/05/18/201207.htmlC在线C在线Sun, 18 May 2008 03:22:00 GMThttp://www.tkk7.com/kxx129/archive/2008/05/18/201207.htmlhttp://www.tkk7.com/kxx129/comments/201207.htmlhttp://www.tkk7.com/kxx129/archive/2008/05/18/201207.html#Feedback0http://www.tkk7.com/kxx129/comments/commentRss/201207.htmlhttp://www.tkk7.com/kxx129/services/trackbacks/201207.html 把最q碰到的 能够用的验证?都放出来Q作个记?
1.GSC_WebControlLibrary q是在网上找到的一个控Ӟ非常好用。但是效果不是特别好Q见下图?br /> Q虽然容易用,所有的属性都可以像控件一栯|,但是可用性不太高。用户不能自定义Q而且看v来这个验证码效果不太好?br /> 效果Q?img alt="" src="http://www.tkk7.com/images/blogjava_net/kxx129/14928/r_t_1.bmp" border="0" />

2.用一个页面生成图片,另一个页面调用,验证码存入cookieQ调用时取cookieҎ验证.q个用户可以按自己的喜好更Ҏ果和验证码的长度?Q:

效果如图Q?br />
代码如下Q?br /> CheckCode.aspx

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public partial class Tools_CheckCode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.CreateCheckCodeImage(GenerateCheckCode());

    }

    private string GenerateCheckCode()
    {
        int number;
        char code;
        string checkCode = String.Empty;

        System.Random random = new Random();

        for (int i = 0; i < 5; i++)
        {
            number = random.Next();

            if (number % 2 == 0)
                code = (char)('0' + (char)(number % 10));
            else
                code = (char)('A' + (char)(number % 26));

            checkCode += code.ToString();
        }

        Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));

        return checkCode;
    }

    private void CreateCheckCodeImage(string checkCode)
    {
        if (checkCode == null || checkCode.Trim() == String.Empty)
            return;

        System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
        Graphics g = Graphics.FromImage(image);

        try
        {
            //生成随机生成?br />             Random random = new Random();

            //清空囄背景?br />             g.Clear(Color.White);

            //d片的背景噪音U?br />             for (int i = 0; i < 25; i++)
            {
                int x1 = random.Next(image.Width);
                int x2 = random.Next(image.Width);
                int y1 = random.Next(image.Height);
                int y2 = random.Next(image.Height);

                g.DrawLine(new Pen(Color.GreenYellow), x1, y1, x2, y2);
            }

            Font font = new System.Drawing.Font("Verdana", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
            g.DrawString(checkCode, font, brush, 2, 2);

            //d片的前景噪音?br />             for (int i = 0; i < 80; i++)
            {
                int x = random.Next(image.Width);
                int y = random.Next(image.Height);

                image.SetPixel(x, y, Color.FromArgb(random.Next()));
            }

            //d片的ҎU?br />             g.DrawRectangle(new Pen(Color.Red), 0, 0, image.Width - 1, image.Height - 1);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            Response.ClearContent();
            Response.ContentType = "image/Gif";
            Response.BinaryWrite(ms.ToArray());
        }
        finally
        {
            g.Dispose();
            image.Dispose();
        }
    }


}

然后在需要用的面引用Q?br /> UseCheckCode.aspx
<img src="Tools/CheckCode.aspx" alt="验证? style="width: 60px; height: 24px" />
3.用web handler生成囄。这个其实和前面的意思大致差不多Q调用方法也基本?一?不同的是Q他的验证码是存入Session的。供学习参考?/p>

效果囑֦下:


ValidateImageHandler.ashx
%@ WebHandler Language="C#" Class="ValidateImageHandler" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;

/**//// <summary>
/// ValidateImageHandler 生成|站验证码功?br /> /// </summary>
public class ValidateImageHandler : IHttpHandler, IRequiresSessionState
{
    int intLength = 5;               //长度
    string strIdentify = "Identify"; //随机字串存储键|以便存储到Session?br />     public ValidateImageHandler()
    {       
    }

    /**//// <summary>
    /// 生成验证囄核心代码
    /// </summary>
    /// <param name="hc"></param>
    public void ProcessRequest(HttpContext hc)
    {
        //讄输出图片格?br />         hc.Response.ContentType = "image/gif";
       
        Bitmap b = new Bitmap(200, 60);
        Graphics g = Graphics.FromImage(b);
        g.FillRectangle(new SolidBrush(Color.YellowGreen), 0, 0, 200, 60);
        Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
        Random r = new Random();

        //合法随机昄字符列表
        string strLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        StringBuilder s = new StringBuilder();
       
        //随机生成的字符串绘制到囄?br />         for (int i = 0; i < intLength; i++)
        {
            s.Append(strLetters.Substring(r.Next(0, strLetters.Length - 1), 1));
            g.DrawString(s[s.Length - 1].ToString(), font, new SolidBrush(Color.Blue), i * 38, r.Next(0, 15));
        }

        //生成q扰U条
        Pen pen = new Pen(new SolidBrush(Color.Blue), 2);
        for (int i = 0; i < 10; i++)
        {
            g.DrawLine(pen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
        }
        b.Save(hc.Response.OutputStream, ImageFormat.Gif);
        hc.Session[strIdentify] = s.ToString(); //先保存在Session中,验证与用戯入是否一?br />         hc.Response.End();
  
    }
   
    /**//// <summary>
    /// 表示此类实例是否可以被多个请求共?重用可以提高性能)
    /// </summary>
    public bool IsReusable
    {
        get
        {
            return true;
        }
    }
}


转自: ASP.NET验证码(3U)

C在线 2008-05-18 11:22 发表评论
]]>
string.format的?/title><link>http://www.tkk7.com/kxx129/archive/2008/02/19/180758.html</link><dc:creator>C在线</dc:creator><author>C在线</author><pubDate>Tue, 19 Feb 2008 15:19:00 GMT</pubDate><guid>http://www.tkk7.com/kxx129/archive/2008/02/19/180758.html</guid><wfw:comment>http://www.tkk7.com/kxx129/comments/180758.html</wfw:comment><comments>http://www.tkk7.com/kxx129/archive/2008/02/19/180758.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/kxx129/comments/commentRss/180758.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/kxx129/services/trackbacks/180758.html</trackback:ping><description><![CDATA[<p><font face="Times New Roman" color="#000000" size="3">stringstr1 =string.Format("{0:N1}",56789);               //result: 56,789.0<br /> stringstr2 =string.Format("{0:N2}",56789);               //result: 56,789.00<br /> stringstr3 =string.Format("{0:N3}",56789);               //result: 56,789.000<br /> stringstr8 =string.Format("{0:F1}",56789);               //result: 56789.0<br /> stringstr9 =string.Format("{0:F2}",56789);               //result: 56789.00<br /> stringstr11 =(56789 / 100.0).ToString("#.##");           //result: 567.89<br /> stringstr12 =(56789 / 100).ToString("#.##");             //result: 567</font></p> <p><font face="Times New Roman" color="#000000" size="3">C ?c<br /> 货币<br /> Console.Write("{0:C}", 2.5); / 2.50<br /> Console.Write("{0:C}", -2.5); //($2.50)</font></p> <p><font face="Times New Roman" color="#000000" size="3">D ?d<br /> 十进制数<br /> Console.Write("{0:D5}", 25); //00025<br /> <br /> E ?e<br /> U学?br /> Console.Write("{0:E}", 250000); //2.500000E+005<br /> <br /> F ?f<br /> 固定?br /> Console.Write("{0:F2}", 25); //25.00<br /> Console.Write("{0:F0}", 25); //25<br /> <br /> G ?g<br /> 常规<br /> Console.Write("{0:G}", 2.5); //2.5<br /> <br /> N ?n<br /> 数字<br /> Console.Write("{0:N}", 2500000); //2,500,000.00<br /> <br /> X ?x<br /> 十六q制<br /> Console.Write("{0:X}", 250); //FA<br /> Console.Write("{0:X}", 0xffff); //FFFF</font></p> <p> </p> <p><font face="Times New Roman" color="#000000" size="3"> <table cellspacing="0" bordercolordark="#ffffff" cellpadding="0" rules="none" width="780" align="center" border="0"> <tbody> <tr> <td></td> </tr> <tr> <td height="50"></td> </tr> <tr> <td> <table cellspacing="0" cellpadding="0" width="760" align="center" border="0"> <tbody> <tr> <td><br /> <h2>C#格式化数值结果表</h2> <div id="ike8q8y" class="postbody"> <table style="width: 474px; border-collapse: collapse; height: 203px" cellspacing="0" cellpadding="3" border="1"> <tbody> <tr bgcolor="#c0c0c0"> <td style="font-size: 12pt"> <p align="center"><font size="2"><strong>字符</strong></font></p> </td> <td style="font-size: 12pt"> <p align="center"><font size="2"><strong>说明</strong></font></p> </td> <td style="font-size: 12pt"> <p align="center"><font size="2"><strong>CZ</strong></font></p> </td> <td style="font-size: 12pt"> <p align="center"><font size="2"><strong>输出</strong></font></p> </td> </tr> <tr> <td>C</td> <td>货币</td> <td><strong style="color: black; background-color: #a0ffff"><font style="background-color: #ffffff">string.Format</font></strong>("{0:C3}", 2)</td> <td>Q?.000</td> </tr> <tr> <td>D</td> <td>十进?/td> <td><font style="background-color: #ffffff"><strong style="color: black; background-color: #a0ffff"><font style="background-color: #ffffff">string.Format</font></strong>("{0:D3}", 2)</font></td> <td><font style="background-color: #ffffff">002</font></td> </tr> <tr> <td><font style="background-color: #ffffff">E</font></td> <td><font style="background-color: #ffffff">U学计数?/font></td> <td><font style="background-color: #ffffff">1.20E+001</font></td> <td><font style="background-color: #ffffff">1.20E+001</font></td> </tr> <tr> <td><font style="background-color: #ffffff">G</font></td> <td><font style="background-color: #ffffff">常规</font></td> <td><font style="background-color: #ffffff"><strong style="color: black; background-color: #a0ffff"><font style="background-color: #ffffff">string.Format</font></strong>("{0:G}", 2)</font></td> <td><font style="background-color: #ffffff">2</font></td> </tr> <tr> <td><font style="background-color: #ffffff">N</font></td> <td><font style="background-color: #ffffff">用分号隔开的数?/font></td> <td><font style="background-color: #ffffff"><strong style="color: black; background-color: #a0ffff"><font style="background-color: #ffffff">string.Format</font></strong>("{0:N}", 250000)</font></td> <td><font style="background-color: #ffffff">250,000.00</font></td> </tr> <tr> <td><font style="background-color: #ffffff">X</font></td> <td><font style="background-color: #ffffff">十六q制</font></td> <td><font style="background-color: #ffffff"><strong style="color: black; background-color: #a0ffff"><font style="background-color: #ffffff">string.Format</font></strong>("{0:X000}", 12)</font></td> <td><font style="background-color: #ffffff">C</font></td> </tr> <tr> <td><font style="background-color: #ffffff"><br /> </font></td> <td><font style="background-color: #ffffff"><br /> </font></td> <td><font style="background-color: #ffffff"><strong style="color: black; background-color: #a0ffff"><font style="background-color: #ffffff">string.Format</font></strong>("{0:000.000}", 12.2)</font></td> <td>012.200</td> </tr> </tbody> </table> </div> <h3><font face="Verdana">Strings</font></h3> <p><font size="2"><font size="3">There really isn't any formatting within a strong, beyond it's alignment. Alignment works for any argument being printed in a String.Format call.</font> </font></p> <p> </p> <p> <table style="width: 520px; height: 62px"> <tbody> <tr> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Sample</font></strong></td> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Generates</font></strong></td> </tr> <tr> <td align="center"><font face="Courier New" size="2">String.Format("->{1,10}<-", "Hello");</font></td> <td><font face="Courier New" size="2">-> Hello<-</font></td> </tr> <tr> <td align="center"><font face="Courier New" size="2">String.Format("->{1,-10}<-", "Hello");</font></td> <td><font face="Courier New" size="2">->Hello <-</font></td> </tr> </tbody> </table> </p> <h3><font face="Verdana">Numbers</font></h3> <p>Basic number formatting specifiers:</p> <p> </p> <p> <table style="width: 631px; height: 342px"> <tbody> <tr> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Specifier</font></strong></td> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Type</font></strong></td> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Format </font></strong></td> <td align="center" bgcolor="#c0c0c0"> <p><strong><font size="2">Output <br /> </font></strong><strong><font size="2">(Passed <br /> Double 1.42)</font></strong></p> </td> <td align="center" bgcolor="#c0c0c0"> <p><strong><font size="2">Output <br /> </font></strong><strong><font size="2">(Passed <br /> Int -12400)</font></strong></p> </td> </tr> <tr> <td align="center"><font size="2">c</font></td> <td><font size="2">Currency</font></td> <td><font face="Courier New" size="2">{0:c}</font></td> <td><font size="2">Q?.42</font></td> <td><font size="2">-Q?2,400</font></td> </tr> <tr> <td align="center"><font size="2">d</font></td> <td><font size="2">Decimal (Whole number)</font></td> <td><font face="Courier New" size="2">{0:d}</font></td> <td><strong><font size="2">System.<br /> FormatException</font></strong></td> <td><font size="2">-12400</font></td> </tr> <tr> <td align="center"><font size="2">e</font></td> <td><font size="2">Scientific</font></td> <td><font face="Courier New" size="2">{0:e}</font></td> <td><font size="2">1.420000e+000</font></td> <td><font size="2">-1.240000e+004</font></td> </tr> <tr> <td align="center"><font size="2">f</font></td> <td><font size="2">Fixed point</font></td> <td><font face="Courier New" size="2">{0:f}</font></td> <td><font size="2">1.42</font></td> <td><font size="2">-12400.00</font></td> </tr> <tr> <td align="center"><font size="2">g</font></td> <td><font size="2">General</font></td> <td><font face="Courier New" size="2">{0:g}</font></td> <td><font size="2">1.42</font></td> <td><font size="2">-12400</font></td> </tr> <tr> <td align="center"><font size="2">n</font></td> <td><font size="2">Number with commas for thousands</font></td> <td><font face="Courier New" size="2">{0:n}</font></td> <td><font size="2">1.42</font></td> <td><font size="2">-12,400</font></td> </tr> <tr> <td align="center"><font size="2">r</font></td> <td><font size="2">Round trippable</font></td> <td><font face="Courier New" size="2">{0:r}</font></td> <td><font size="2">1.42</font></td> <td><strong><font size="2">System.<br /> FormatException</font></strong></td> </tr> <tr> <td align="center"><font size="2">x</font></td> <td><font size="2">Hexadecimal</font></td> <td><font face="Courier New" size="2">{0:x4}</font></td> <td><strong><font size="2">System.<br /> FormatException</font></strong></td> <td><font size="2">cf90</font></td> </tr> </tbody> </table> </p> <p>Custom number formatting:</p> <p> </p> <p> <table style="width: 681px; height: 476px"> <tbody> <tr> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Specifier</font></strong></td> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Type</font></strong></td> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Example </font></strong></td> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Output (Passed Double 1500.42)</font></strong></td> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Note</font></strong></td> </tr> <tr> <td align="center"><font size="2">0</font></td> <td><font size="2">Zero placeholder</font></td> <td align="center"><font face="Courier New" size="2">{0:00.0000}</font></td> <td><font size="2">1500.4200</font></td> <td><font size="2">Pads with zeroes.</font></td> </tr> <tr> <td align="center"><font size="2">#</font></td> <td><font size="2">Digit placeholder</font></td> <td align="center"><font face="Courier New" size="2">{0:(#).##}</font></td> <td><font size="2">(1500).42</font></td> <td></td> </tr> <tr> <td align="center"><font size="2">.</font></td> <td><font size="2">Decimal point</font></td> <td align="center"><font face="Courier New" size="2">{0:0.0}</font></td> <td><font size="2">1500.4</font></td> <td></td> </tr> <tr> <td align="center"><font size="2">,</font></td> <td><font size="2">Thousand separator</font></td> <td align="center"><font face="Courier New" size="2">{0:0,0}</font></td> <td><font size="2">1,500</font></td> <td><font size="2">Must be between two zeroes.</font></td> </tr> <tr> <td align="center"><font size="2">,.</font></td> <td><font size="2">Number scaling</font></td> <td align="center"><font size="2"><font face="Courier New">{0:0,.}</font> </font></td> <td><font size="2">2</font></td> <td><font size="2">Comma adjacent to Period scales by 1000.</font></td> </tr> <tr> <td align="center"><font size="2">%</font></td> <td><font size="2">Percent</font></td> <td align="center"><font face="Courier New" size="2">{0:0%}</font></td> <td><font size="2">150042%</font></td> <td><font size="2">Multiplies by 100, adds % sign.</font></td> </tr> <tr> <td align="center"><font size="2">e</font></td> <td><font size="2">Exponent placeholder</font></td> <td align="center"><font face="Courier New" size="2">{0:00e+0}</font></td> <td><font size="2">15e+2</font></td> <td><font size="2">Many exponent formats available.</font></td> </tr> <tr> <td align="center"><font size="2">;</font></td> <td><font size="2">Group separator</font></td> <td align="center"><font size="2">see below</font></td> <td></td> <td></td> </tr> </tbody> </table> </p> <p>The group separator is especially useful for formatting currency values which require that negative values be enclosed in parentheses. This currency formatting example at the bottom of this document makes it obvious:</p> <h3><font face="Verdana">Dates</font></h3> <p>Note that date formatting is especially dependant on the system's regional settings; the example strings here are from my local locale.</p> <p> </p> <p> <table style="width: 675px; height: 334px"> <tbody> <tr> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Specifier</font></strong></td> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Type</font></strong></td> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Example (Passed System.DateTime.Now)</font></strong></td> </tr> <tr> <td align="center"><font size="2">d</font></td> <td><font size="2">Short date</font></td> <td><font size="2">10/12/2002</font></td> </tr> <tr> <td align="center"><font size="2">D</font></td> <td><font size="2">Long date</font></td> <td><font size="2">December 10, 2002</font></td> </tr> <tr> <td align="center"><font size="2">t</font></td> <td><font size="2">Short time</font></td> <td><font size="2">10:11 PM</font></td> </tr> <tr> <td align="center"><font size="2">T</font></td> <td><font size="2">Long time</font></td> <td><font size="2">10:11:29 PM</font></td> </tr> <tr> <td align="center"><font size="2">f</font></td> <td><font size="2">Full date & time </font></td> <td><font size="2">December 10, 2002 10:11 PM</font></td> </tr> <tr> <td align="center"><font size="2">F</font></td> <td><font size="2">Full date & time (long)</font></td> <td><font size="2">December 10, 2002 10:11:29 PM</font></td> </tr> <tr> <td align="center"><font size="2">g</font></td> <td><font size="2">Default date & time</font></td> <td><font size="2">10/12/2002 10:11 PM</font></td> </tr> <tr> <td align="center"><font size="2">G</font></td> <td><font size="2">Default date & time (long)</font></td> <td><font size="2">10/12/2002 10:11:29 PM</font></td> </tr> <tr> <td align="center"><font size="2">M</font></td> <td><font size="2">Month day pattern</font></td> <td><font size="2">December 10</font></td> </tr> <tr> <td align="center"><font size="2">r</font></td> <td><font size="2">RFC1123 date string</font></td> <td><font size="2">Tue, 10 Dec 2002 22:11:29 GMT</font></td> </tr> <tr> <td align="center"><font size="2">s</font></td> <td><font size="2">Sortable date string</font></td> <td><font size="2">2002-12-10T22:11:29</font></td> </tr> <tr> <td align="center"><font size="2">u</font></td> <td><font size="2">Universal sortable, local time</font></td> <td><font size="2">2002-12-10 22:13:50Z</font></td> </tr> <tr> <td align="center"><font size="2">U</font></td> <td><font size="2">Universal sortable, GMT</font></td> <td><font size="2">December 11, 2002 3:13:50 AM</font></td> </tr> <tr> <td align="center"><font size="2">Y</font></td> <td><font size="2">Year month pattern</font></td> <td><font size="2">December, 2002</font></td> </tr> </tbody> </table> </p> <p>The 'U' specifier seems broken; that string certainly isn't sortable.</p> <p><strong>Custom date formatting:</strong></p> <p> </p> <p> <table style="width: 721px; height: 442px"> <tbody> <tr> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Specifier</font></strong></td> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Type</font></strong></td> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Example </font></strong></td> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Example Output</font></strong></td> </tr> <tr> <td align="center"><font size="2">dd</font></td> <td><font size="2">Day</font></td> <td align="center"><font face="Courier New" size="2">{0:dd}</font></td> <td><font size="2">10</font></td> </tr> <tr> <td align="center"><font size="2">ddd</font></td> <td><font size="2">Day name</font></td> <td align="center"><font face="Courier New" size="2">{0:ddd}</font></td> <td><font size="2">Tue</font></td> </tr> <tr> <td align="center"><font size="2">dddd</font></td> <td><font size="2">Full day name</font></td> <td align="center"><font face="Courier New" size="2">{0:dddd}</font></td> <td><font size="2">Tuesday</font></td> </tr> <tr> <td align="center"><font size="2">f, ff, ...</font></td> <td><font size="2">Second fractions</font></td> <td align="center"><font face="Courier New" size="2">{0:fff}</font></td> <td><font size="2">932</font></td> </tr> <tr> <td align="center"><font size="2">gg, ...</font></td> <td><font size="2">Era</font></td> <td align="center"><font face="Courier New" size="2">{0:gg}</font></td> <td><font size="2">A.D.</font></td> </tr> <tr> <td align="center"><font size="2">hh</font></td> <td><font size="2">2 digit hour</font></td> <td align="center"><font face="Courier New" size="2">{0:hh}</font></td> <td><font size="2">10</font></td> </tr> <tr> <td align="center"><font size="2">HH</font></td> <td><font size="2">2 digit hour, 24hr format</font></td> <td align="center"><font face="Courier New" size="2">{0:HH}</font></td> <td><font size="2">22</font></td> </tr> <tr> <td align="center"><font size="2">mm</font></td> <td><font size="2">Minute 00-59</font></td> <td align="center"><font face="Courier New" size="2">{0:mm}</font></td> <td><font size="2">38</font></td> </tr> <tr> <td align="center"><font size="2">MM</font></td> <td><font size="2">Month 01-12</font></td> <td align="center"><font face="Courier New" size="2">{0:MM}</font></td> <td><font size="2">12</font></td> </tr> <tr> <td align="center"><font size="2">MMM</font></td> <td><font size="2">Month abbreviation</font></td> <td align="center"><font face="Courier New" size="2">{0:MMM}</font></td> <td><font size="2">Dec</font></td> </tr> <tr> <td align="center"><font size="2">MMMM</font></td> <td><font size="2">Full month name</font></td> <td align="center"><font face="Courier New" size="2">{0:MMMM}</font></td> <td><font size="2">December</font></td> </tr> <tr> <td align="center"><font size="2">ss</font></td> <td><font size="2">Seconds 00-59</font></td> <td align="center"><font face="Courier New" size="2">{0:ss}</font></td> <td><font size="2">46</font></td> </tr> <tr> <td align="center"><font size="2">tt</font></td> <td><font size="2">AM or PM</font></td> <td align="center"><font face="Courier New" size="2">{0:tt}</font></td> <td><font size="2">PM</font></td> </tr> <tr> <td align="center"><font size="2">yy</font></td> <td><font size="2">Year, 2 digits</font></td> <td align="center"><font face="Courier New" size="2">{0:yy}</font></td> <td><font size="2">02</font></td> </tr> <tr> <td align="center"><font size="2">yyyy</font></td> <td><font size="2">Year</font></td> <td align="center"><font face="Courier New" size="2">{0:yyyy}</font></td> <td><font size="2">2002</font></td> </tr> <tr> <td align="center"><font size="2">zz</font></td> <td><font size="2">Timezone offset, 2 digits</font></td> <td align="center"><font face="Courier New" size="2">{0:zz}</font></td> <td><font size="2">-05</font></td> </tr> <tr> <td align="center"><font size="2">zzz</font></td> <td><font size="2">Full timezone offset</font></td> <td align="center"><font face="Courier New" size="2">{0:zzz}</font></td> <td><font size="2">-05:00</font></td> </tr> <tr> <td align="center"><font size="2">:</font></td> <td><font size="2">Separator</font></td> <td align="center"><font face="Courier New" size="2">{0:hh:mm:ss}</font></td> <td><font size="2">10:43:20</font></td> </tr> <tr> <td align="center"><font size="2">/</font></td> <td><font size="2">Separator</font></td> <td align="center"><font face="Courier New" size="2">{0:dd/MM/yyyy}</font></td> <td><font size="2">10/12/2002</font></td> </tr> </tbody> </table> </p> <h3><font face="Verdana">Enumerations</font></h3> <p> </p> <p> <table style="width: 702px; height: 102px"> <tbody> <tr> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Specifier</font></strong></td> <td align="center" bgcolor="#c0c0c0"><strong><font size="2">Type</font></strong></td> </tr> <tr> <td align="center"><font size="2">g</font></td> <td><font size="2">Default (Flag names if available, otherwise decimal)</font></td> </tr> <tr> <td align="center"><font size="2">f</font></td> <td><font size="2">Flags always</font></td> </tr> <tr> <td align="center"><font size="2">d</font></td> <td><font size="2">Integer always</font></td> </tr> <tr> <td align="center"><font size="2">x</font></td> <td><font size="2">Eight digit hex.</font></td> </tr> </tbody> </table> </p> <h3><font face="Verdana">Some Useful Examples</font></h3> <p><font face="Courier New" size="2">String.Format("{0:Q?,##0.00;(Q?,##0.00);Zero}", value);</font></p> <blockquote dir="ltr" style="margin-right: 0px"> <p>This will output "Q?,240.00" if passed 1243.50. It will output the same format but in parentheses if the number is negative, and will output the string "Zero" if the number is zero.</p> </blockquote> <p><font face="Courier New" size="2">String.Format("{0:(###) ###-####}", 18005551212);</font></p> <blockquote dir="ltr" style="margin-right: 0px"> <p>This will output "(800) 555-1212".</p> <p> </p> <p><font size="5"><strong>变量.ToString()</strong></font></p> <p><br /> 字符型{?转ؓ字符?<br /> 12345.ToString("n"); //生成 12,345.00 <br /> 12345.ToString("C"); //生成 K?2,345.00 <br /> 12345.ToString("e"); //生成 1.234500e+004 <br /> 12345.ToString("f4"); //生成 12345.0000 <br /> 12345.ToString("x"); //生成 3039 (16q制) <br /> 12345.ToString("p"); //生成 1,234,500.00%</p> </blockquote></td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </font></p> <img src ="http://www.tkk7.com/kxx129/aggbug/180758.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/kxx129/" target="_blank">C在线</a> 2008-02-19 23:19 <a href="http://www.tkk7.com/kxx129/archive/2008/02/19/180758.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Log4Net使用指南http://www.tkk7.com/kxx129/archive/2008/02/19/180738.htmlC在线C在线Tue, 19 Feb 2008 12:16:00 GMThttp://www.tkk7.com/kxx129/archive/2008/02/19/180738.htmlhttp://www.tkk7.com/kxx129/comments/180738.htmlhttp://www.tkk7.com/kxx129/archive/2008/02/19/180738.html#Feedback0http://www.tkk7.com/kxx129/comments/commentRss/180738.htmlhttp://www.tkk7.com/kxx129/services/trackbacks/180738.html

Log4Net使用指南

1           ?

1.1          Log4net的优点:

几乎所有的大型应用都会有自q用于跟踪调试的API。因Z旦程序被部v以后Q就不太可能再利用专门的调试工具了。然而一个管理员可能需要有一套强大的日志pȝ来诊断和修复配置上的问题?

l验表明Q日志记录往往是Y件开发周期中的重要组成部分。它h以下几个优点Q它可以提供应用E序q行时的_环境Q可供开发h员尽快找到应用程序中的BugQ一旦在E序中加入了Log 输出代码Q程序运行过E中p生成q输出日志信息而无需人工q预。另外,日志信息可以输出C同的地方Q控制台Q文件等Q以备以后研I之用?

Log4net是样一个目的设计的Q用?NET开发环境的日志记录包?

1.2          Log4net的安装:

用户可以?a >http://logging.apache.org/log4net/下蝲log4net的源代码。解压Y件包后,在解压的src目录下将log4net.sln载入Visual Studio .NETQ编译后可以得到log4net.dll。用戯在自qE序里加入日志功能,只需log4net.dll引入工程卛_?nbsp; 

2           Log4net的结?

log4net 有四U主要的lgQ分别是LoggerQ记录器Q? RepositoryQ库Q? AppenderQ附着器)以及 LayoutQ布局Q?

2.1          Logger

2.1.1     Logger接口

Logger是应用程序需要交互的主要lgQ它用来产生日志消息。生的日志消息q不直接昄Q还要预先经qLayout的格式化处理后才会输出?

Logger提供了多U方式来记录一个日志消息,你可以在你的应用E序里创建多个LoggerQ每个实例化的Logger对象都被log4net框架作ؓ命名实体(named entity)来维护。这意味着Z重用Logger对象Q你不必它在不同的cL对象间传递,只需要用它的名字为参数调用就可以了。log4net框架使用l承体系Q承体pȝg.NET中的名字I间。也是_如果有两个logger,分别被定义ؓa.b.c和a.bQ那么我们说a.b是a.b.c的祖先。每一个logger都承了先的属?

Log4net框架定义了一个ILog接口Q所有的loggerc都必须实现q个接口。如果你惛_C个自定义的loggerQ你必须首先实现q个接口。你可以参考在/extension目录下的几个例子?

ILog接口的定义如下:

public interface ILog

{

  void Debug(object message);

  void Info(object message);

  void Warn(object message);

  void Error(object message);

  void Fatal(object message);

 

//以上的每一个方法都有一个重载的ҎQ用来支持异常处理?

//每一个重载方法都如下所C,有一个异常类型的附加参数?

  void Debug(object message, Exception ex);

  // ...

 

//Boolean 属性用来检查Logger的日志?

//Q我们马上会在后面看到日志别)

  bool isDebugEnabled;

  bool isInfoEnabled;

  //… 其他Ҏ对应的Boolean属?

}

Log4net框架定义了一个叫做LogManager的类Q用来管理所有的logger对象。它有一个GetLogger()静态方法,用我们提供的名字参数来检索已l存在的Logger对象。如果框枉不存在该Logger对象Q它也会为我们创Z个Logger对象。代码如下所C:

log4net.ILog log = log4net.LogManager.GetLogger("logger-name");

通常来说Q我们会以类QclassQ的cdQtypeQؓ参数来调用GetLogger()Q以便跟t我们正在进行日志记录的cR传递的c?class)的类?type)可以用typeof(Classname)Ҏ来获得,或者可以用如下的反方法来获得Q?

System.Reflection.MethodBase.GetCurrentMethod().DeclaringType

管W号长了一些,但是后者可以用于一些场合,比如获取调用Ҏ的类(class)的类?type)?

2.1.2     日志的?

正如你在ILog的接口中看到的一P有五U不同的Ҏ可以跟踪一个应用程序。事实上Q这五种Ҏ是运作在Logger对象讄的不同日志优先别上。这几种不同的别是作ؓ帔R定义在log4net.spi.LevelcM。你可以在程序中使用M一U方法。但是在最后的发布中你也许不想让所有的代码来浪费你的CPU周期Q因此,框架提供?U别和相应的Boolean属性来控制日志记录的类型?

 

 

Level有以下几U取?

U别
 允许的方?
 Boolean属?
 优先U别
 
OFF
  
  
 Highest
 
FATAL
 void Fatal(...);
 bool IsFatalEnabled;
  
 
RROR
 void Error(...);
 bool IsErrorEnabled;
  
 
WARN
 void Warn(...);
 bool IsWarnEnabled;
  
 
INFO
 void Info(...);
 bool IsInfoEnabled;
  
 
DEBUG
 void Debug(...);
 bool IsDebugEnabled;
  
 
ALL
  
  
 Lowest
 

?  Logger的日志?

在log4net框架里,通过讄配置文gQ每个日志对象都被分配了一个日志优先别。如果没有给一个日志对象显式地分配一个别,那么该对象会试图从他的祖先承一个别倹{?

ILog接口的每个方法都有一个预先定义好了的U别倹{正如你在表1看到的,ILog的Inof()ҎhINFOU别。同LQ以此类推,Error()ҎhERRORU别。当我们使用以上的Q何一U方法时Qlog4net框架会检查日志对象logger的别和Ҏ的别。只有当Ҏ的别高于日志别时Q日志请求才会被接受q执行?

举例说明Q当你创Z一个日志对象,q且把他的别设|ؓINFO。于是框架会讄日志的每个Boolean属性。当你调用相应的日志ҎӞ框架会检查相应的Boolean属性,以决定该Ҏ能不能执行。如下的代码Q?

 

Logger.Info("message");

Logger.Debug("message");

Logger.Warn("message");

对于W一U方法,Info()的别等与日志的U别QINFOQ,因此日志h会被传递,我们可以得到输出l果”message”?

对于W二U方法,Debug()的别低于日志对象logger的日志?INFO)Q因此,日志h被拒l了Q我们得不到M输出。同LQ针对第三行语句Q我们可以很Ҏ得出l论?

在表1中有两个Ҏ的别:ALL和OFF。ALL表示允许所有的日志h。OFF是拒l所有的h?

你也可以昑ּ地检查Logger对象的Boolean属性,如下所C:

if (logger.IsDebugEnabled)

{

  Logger.Debug("message");

}

 

2.2          Repository

Repository主要用于负责日志对象l织l构的维护。在log4net的以前版本中Q框架仅支持分等U的l织l构(hierarchical organization)。这U等U结构本质上是库的一个实玎ͼq且定义在log4net.Repository.Hierarchy 名字I间中。要实现一个RepositoryQ需要实现log4net.Repository.ILoggerRepository 接口。但是通常q不是直接实现该接口Q而是以log4net.Repository.LoggerRepositorySkeleton为基cȝѝ体pd (hierarchical repository )则由log4net.Repository.Hierarchy.Hierarchycd现?

如果你是个log4net框架的用者,而非扩展者,那么你几乎不会在你的代码里用到Repository的类。相反的Q你需要用到LogManagercL自动理库和日志对象?

2.3          Appender

一个好的日志框架应该能够生多目的地的输出。比如说输出到控制台或保存到一个日志文件。log4net 能够很好的满些要求。它使用一个叫做Appender的组件来定义输出介质。正如名字所C,q些lg把它们附加到Logger日志lg上ƈ输Z递到输出中。你可以把多个Appenderlg附加C个日志对象上?Log4net框架提供了几个Appenderlg。关于log4net提供的Appenderlg的完整列表可以在log4net框架的帮助手册中扑ֈ。有了这些现成的AppenderlgQ一般来说你没有必要再自q写了。但是如果你愿意Q可以从log4net.Appender.AppenderSkeletoncȝѝ?

2.4          Appender Filters

一个Appender 对象~省地将所有的日志事g传递到输出。Appender的过滤器(Appender Filters) 可以按照不同的标准过滤日志事件。在log4net.Filter的名字空间下已经有几个预定义的过滤器。用这些过滤器Q你可以按照日志U别范围qo日志事gQ或者按照某个特D的字符串进行过滤。你可以在API的帮助文件中发现更多关于qo器的信息?

2.5          Layout

Layout lg用于向用hC最后经q格式化的输Z息。输Z息可以以多种格式昄Q主要依赖于我们采用的Layoutlgcd。可以是U性的或一个XML文g。Layoutlg和一个Appenderlg一起工作。API帮助手册中有关于不同Layoutlg的列表。一个Appender对象Q只能对应一个Layout对象。要实现你自qLayoutc,你需要从log4net.Layout.LayoutSkeletoncȝ承,它实CILayout接口?

 

 

3           在程序中使用log4net

在开始对你的E序q行日志记录前,需要先启动log4net引擎。这意味着你需要先配置前面提到的三U组件。你可以用两U方法来讑֮配置Q在单独的文件中讑֮配置或在代码中定义配|?

因ؓ下面几种原因Q推荐在一个单独的文g中定义配|:

l         你不需要重新编译源代码p改变配置Q?

l         你可以在E序正运行的时候就改变配置。这一点在一些WEBE序和远E过E调用的E序中有时很重要Q?

考虑到第一U方法的重要性,我们先看看怎样在文件中讑֮配置信息?

3.1          定义配置文g

配置信息可以攑֜如下几种形式文g的一U中?

在程序的配置文g里,如AssemblyName.config 或web.config.

在你自己的文仉。文件名可以是Q何你惌的名字,如AppName.exe.xyz{?

log4net框架会在相对于AppDomain.CurrentDomain.BaseDirectory 属性定义的目录路径下查N|文件。框架在配置文g里要查找的唯一标识?lt;log4net>标签。一个完整的配置文g的例子如下:

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <configSections>

    <section name="log4net"

      type="log4net.Config.Log4NetConfigurationSectionHandler,

            log4net-net-1.0"

    />

  </configSections>

 

  <log4net>

 

    <root>

      <level value="WARN" />

      <appender-ref ref="LogFileAppender" />

      <appender-ref ref="ConsoleAppender" />

    </root>

 

    <logger name="testApp.Logging">

      <level value="DEBUG"/>

    </logger>

   

    <appender name="LogFileAppender"

             type="log4net.Appender.FileAppender" >

      <param name="File" value="log-file.txt" />

      <param name="AppendToFile" value="true" />

 

      <layout type="log4net.Layout.PatternLayout">

        <param name="Header" value="[Header]\r\n"/>

        <param name="Footer" value="[Footer]\r\n"/>

        <param name="ConversionPattern"

           value="%d [%t] %-5p %c [%x]  - %m%n"

         />

      </layout>

 

      <filter type="log4net.Filter.LevelRangeFilter">

        <param name="LevelMin" value="DEBUG" />

        <param name="LevelMax" value="WARN" />

      </filter>

    </appender>

   

    <appender name="ConsoleAppender"

              type="log4net.Appender.ConsoleAppender" >

      <layout type="log4net.Layout.PatternLayout">

        <param name="ConversionPattern"  value="%d [%t] %-5p %c [%x] - %m%n"

        />

      </layout>

    </appender>

   

  </log4net>

</configuration>

你可以直接将上面的文本拷贝到ME序中用,但是最好还是能够理解配|文件是怎样构成的?只有当你需要在应用E序配置文g中用log4net配置Ӟ才需要在<configSection>标签中加?lt;section>配置节点入口。对于其他的单独文gQ只?lt;log4net>标签内的文本才是必需的,q些标签的顺序ƈ不是固定的。下面我们依ơ讲解各个标{ֆ文本的含义:

3.1.1     <root>

<root>

  <level value="WARN" />

  <appender-ref ref="LogFileAppender" />

  <appender-ref ref="ConsoleAppender" />

</root>

 

在框架的体系里,所有的日志对象都是Ҏ?root logger)的后代?因此如果一个日志对象没有在配置文g里显式定义,则框架用根日志中定义的属性。在<root>标签里,可以定义levelU别值和Appender的列表。如果没有定义LEVEL的|则缺省ؓDEBUG。可以通过<appender-ref>标签定义日志对象使用的Appender对象?lt;appender-ref>声明了在其他地方定义的Appender对象的一个引用。在一个logger对象中的讄会覆盖根日志的设|。而对Appender属性来_子日志对象则会承父日志对象的Appender列表。这U缺省的行ؓ方式也可以通过昑ּ地设?lt;logger>标签的additivity属性ؓfalse而改变?

<logger name="testApp.Logging" additivity="false">

</logger>

Additivity的值缺省是true.

 

 

3.1.2     <Logger>

<logger name="testApp.Logging">

  <level value="DEBUG"/>

</logger>

 

<logger> 元素预定义了一个具体日志对象的讄。然后通过调用LogManager.GetLogger(“testAPP.Logging”)函数Q你可以索具有该名字的日志。如果LogManager.GetLogger(…)打开的不是预定义的日志对象,则该日志对象会承根日志对象的属性。知道了q一点,我们可以_其实<logger>标签q不是必ȝ?

 

 

3.1.3     <appender>

 

<appender name="LogFileAppender"

          type="log4net.Appender.FileAppender" >

  <param name="File" value="log-file.txt" />

  <param name="AppendToFile" value="true" />

  <layout type="log4net.Layout.PatternLayout">

    <param name="Header" value="[Header]\r\n" />

    <param name="Footer" value="[Footer]\r\n"/>

    <param name="ConversionPattern"

      value="%d [%t] %-5p %c - %m%n"

    />

  </layout>

  <filter type="log4net.Filter.LevelRangeFilter">

    <param name="LevelMin" value="DEBUG" />

    <param name="LevelMax" value="WARN" />

  </filter>

</appender>

 

?lt;root>标签或单个的<logger>标签里的Appender对象可以?lt;appender>标签定义?lt;appender>标签的基本Ş式如上面所C。它定义了appender的名字和cd?另外比较重要的是<appender>标签内部的其他标{。不同的appender有不同的<param>标签。在q里Qؓ了用FileAppender,你需要一个文件名作ؓ参数。另外还需要一个在<appender>标签内部定义一个Layout对象。Layout对象定义在它自己?lt;layout>标签内?lt;layout>标签的type属性定义了Layout的类?在本例里是PatternLayout)Q同时也定了需要提供的参数倹{Header和Footer标签提供了一个日志会?logging session)开始和l束时输出的文字。有xUappender的具体配|的例子Q可以在log4net\doc\manual\example-config-appender.html中得到?

 

3.1.4     log4net.Layout.PatternLayout中的转换模式(ConversionPattern)

%m(message):输出的日志消息,如ILog.Debug(…)输出的一条消?

%n(new line):换行

%d(datetime):输出当前语句q行的时?

%r(run time):输出E序从运行到执行到当前语句时消耗的毫秒?

%t(thread id):当前语句所在的U程ID

%p(priority): 日志的当前优先别,即DEBUG、INFO、WARN…{?

%c(class):当前日志对象的名Uͼ例如Q?

       模式字符串ؓQ?-10c -%m%n

       代码为:

ILog log=LogManager.GetLogger(“Exam.Log”);

log.Debug(“Hello”);

    则输Zؓ下面的Ş式:

Exam.Log       - Hello

%LQ输句所在的行号

%FQ输句所在的文g?

%-数字Q表C的最长度,如果不够Q则用空格填?

例如Q{换模式ؓ%r [%t]%-5p %c - %m%n ?PatternLayout 生成类g以下内容的输出:

176 [main] INFO  org.foo.Bar - Located nearest gas station.

 

3.1.5     <filter>

最后,让我们看看在Appender元素里的<filter>标签。它定义了应用到Appender对象的过滤器。本例中Q我们用了LevelRangeFilterqo?它可以只记录LevelMin和LevelMax参数指定的日志别之间的日志事g。可以在一个Appender上定义多个过滤器QFilterQ?q些qo器将会按照它们定义的序Ҏ志事件进行过滤。其他过滤器的有关信息可以在log4net的SDK文档中找到?

 

3.2          使用配置文g

3.2.1     兌配置文g

当我们创Z上面的配|文件后Q我们接下来需要把它和我们的应用联pv来。缺省的Q每个独立的可执行程序集都会定义它自q配置。log4net框架使用 log4net.Config.DOMConfiguratorAttribute在程序集的别上定义配置文g?/p>

例如Q可以在目的AssemblyInfo.cs文g里添加以下的语句

[assembly:log4net.Config.DOMConfigurator(ConfigFile="filename",

  ConfigFileExtension="ext",Watch=true/false)]

l         ConfigFile:指出了我们的配置文g的\径及文g名,包括扩展名?

l         ConfigFileExtension:如果我们对被~译E序的程序集使用了不同的文g扩展名,那么我们需要定义这个属性,~省的,E序集的配置文g扩展名ؓ”config”?

l         Watch (Boolean属?: log4net框架用这个属性来定是否需要在q行时监视文件的改变。如果这个属性ؓtrue,那么FileSystemWatcher会被用来监视文件的改变Q重命名Q删除等事g?

其中QConfigFile和ConfigFileExtension属性不能同时用,ConfigFile指出了配|文件的名字Q例如,ConfigFile=”Config.txt”

ConfigFileExtension则是指明了和可执行程序集同名的配|文件的扩展名,例如Q应用程序的名称?#8221;test.exe”,ConfigFileExtension=”txt”,则配|文件就应该?#8221;test.exe.txt” Q?

也可以不带参数应用DOMConfiguratio():

 [assembly: log4net.Config.DOMConfigurator()]

也可以在E序代码中用DOMConfiguratorcL开配置文g。类的构造函数需要一个FileInfo对象作参敎ͼ以指打开的配|文件名?q个Ҏ和前面在E序集里讄属性打开一个配|文件的效果是一L?

log4net.Config.DOMConfigurator.Configure(

  new FileInfo("TestLogger.Exe.Config"));

DOMConfigurator c还有一个方法ConfigureAndWatch(..), 用来配置框架q检文件的变化?

以上的步骤ȝ了和配置相关的各个方面,下面我们分两步来用logger对象?

3.2.2     创徏或获取日志对?

日志对象会用在配置文g里定义的属性。如果某个日志对象没有事先在配置文g里定义,那么框架会根据承结构获取祖先节点的属性,最l的Q会从根日志获取属性。如下所C:

Log4net.ILog log = Log4net.LogManager.GetLogger("MyLogger");

3.2.3     输出日志信息

可以使用ILog的几U方法输出日志信息。你也可以在调用某方法前先检查IsXXXEnabled布尔变量Q再军_是否调用输出日志信息的函敎ͼq样可以提高E序的性能。因为框架在调用如ILog.Debug(…)q样的函数时Q也会先判断是否满Level日志U别条g?

if (log.IsDebugEnabled) log.Debug("message");

if (log.IsInfoEnabled) log.Info("message);

 

3.3          在程序中配置log4net

除了前面讲的用一个配|文件来配置log4net以外Q还可以在程序中用代码来配置log4net框架。如下面的例?

 

// 和PatternLayout一起用FileAppender

log4net.Config.BasicConfigurator.Configure(

  new log4net.Appender.FileAppender(

     new log4net.Layout.PatternLayout("%d

       [%t]%-5p %c [%x] - %m%n"),"testfile.log"));

 

// using a FileAppender with an XMLLayout

log4net.Config.BasicConfigurator.Configure(

  new log4net.Appender.FileAppender(

    new log4net.Layout.XMLLayout(),"testfile.xml"));

 

// using a ConsoleAppender with a PatternLayout

log4net.Config.BasicConfigurator.Configure(

  new log4net.Appender.ConsoleAppender(

    new log4net.Layout.PatternLayout("%d

      [%t] %-5p %c - %m%n")));

 

// using a ConsoleAppender with a SimpleLayout

log4net.Config.BasicConfigurator.Configure(

  new log4net.Appender.ConsoleAppender(new

    log4net.Layout.SimpleLayout()));

管q里用代码配|log4net也很方便Q但是你却不能分别配|每个日志对象。所有的q些配置都是被应用到Ҏ志上的?

log4net.Config.BasicConfigurator cM用静态方法Configure 讄一个Appender 对象。而Appender的构造函数又会相应的要求Layout对象。你也可以不带参数直接调用BasicConfigurator.Configure()Q它会用一个缺省的PatternLayout对象Q在一个ConsoleAppender中输Z息。如下所C:

 

log4net.Config.BasicConfigurator.Configure();

在输出时会显C如下格式的信息Q?

 

0 [1688] DEBUG log1 A B C - Test

20 [1688] INFO log1 A B C - Test

当log4net框架被配|好以后Q就可以如前所qC用日志功能了?



C在线 2008-02-19 20:16 发表评论
]]>
վ֩ģ壺 һػ¼Ƶ| ޹ƷYW߹ۿ| wwwһwww| 뾫Ʒ| 91޾ƷƵ| պ޴߶ȸ| ߹ۿ| ߹ۿѳ| hƵѹۿ| ƬѸ | ձһ߲Ͷ| þþþ޾ƷŮ| þþþޱٸ| vƬ߹ۿƷ| ˻ƶվƵ | պѸƵ| պavһ| Ļ| þþƷӰѶ| ߹ۿѻվ| վѴȫպ| С˵| ƷþþþþС˵| ޸һƷ| þþƷav鶹ͼƬ| ޾ƷһۺϾƷ| ͵͵Ʒ| ٸĻӰ| ۺƵ| ŮƵһ| ޲122021鶹| Ƶ| Դ°߹ۿ| Ļ| ŷ պ ۺ| ҹɼӰԺ| ޹bvվ| avƷר߹ۿ| һþ| Ʒ˿| 337Pձŷ޴󵨾Ʒ|