DebuggerWriter

本文介绍了一个自定义的调试记录工具DebuggerWriter,该工具通过不同级别和类别来记录调试信息,并展示了如何在验证用户名密码正确的业务逻辑中使用此工具。

 

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Diagnostics;
using  System.IO;
using  System.Globalization;

namespace  HandFree.DAL
{
    
public   class  DebuggerWriter : TextWriter
    {
        
private   bool  isOpen;
        
private   static  UnicodeEncoding encoding;
        
private   readonly   int  level;
        
private   readonly   string  category;

        
///   <summary>
        
///  Initializes a new instance of the  <see cref="DebuggerWriter"/>  class.
        
///   </summary>
         public  DebuggerWriter()
            : 
this ( 0 , Debugger.DefaultCategory)
        {
        }

        
///   <summary>
        
///  Initializes a new instance of the  <see cref="DebuggerWriter"/>  class with the specified level and category.
        
///   </summary>
        
///   <param name="level"> A description of the importance of the messages. </param>
        
///   <param name="category"> The category of the messages. </param>
         public  DebuggerWriter( int  level,  string  category)
            : 
this (level, category, CultureInfo.CurrentCulture)
        {
        }

        
///   <summary>
        
///  Initializes a new instance of the  <see cref="DebuggerWriter"/>  class with the specified level, category and format provider.
        
///   </summary>
        
///   <param name="level"> A description of the importance of the messages. </param>
        
///   <param name="category"> The category of the messages. </param>
        
///   <param name="formatProvider"> An  <see cref="IFormatProvider"/>  object that controls formatting. </param>
         public  DebuggerWriter( int  level,  string  category, IFormatProvider formatProvider)
            : 
base (formatProvider)
        {
            
this .level  =  level;
            
this .category  =  category;
            
this .isOpen  =   true ;
        }

        
protected   override   void  Dispose( bool  disposing)
        {
            isOpen 
=   false ;
            
base .Dispose(disposing);
        }

        
public   override   void  Write( char  value)
        {
            
if  ( ! isOpen)
            {
                
throw   new  ObjectDisposedException( null );
            }
            Debugger.Log(level, category, value.ToString());
        }

        
public   override   void  Write( string  value)
        {
            
if  ( ! isOpen)
            {
                
throw   new  ObjectDisposedException( null );
            }
            
if  (value  !=   null )
            {
                Debugger.Log(level, category, value);
            }
        }

        
public   override   void  Write( char [] buffer,  int  index,  int  count)
        {
            
if  ( ! isOpen)
            {
                
throw   new  ObjectDisposedException( null );
            }
            
if  (buffer  ==   null   ||  index  <   0   ||  count  <   0   ||  buffer.Length  -  index  <  count)
            {
                
base .Write(buffer, index, count);  //  delegate throw exception to base class
            }
            Debugger.Log(level, category, 
new   string (buffer, index, count));
        }

        
public   override  Encoding Encoding
        {
            
get
            {
                
if  (encoding  ==   null )
                {
                    encoding 
=   new  UnicodeEncoding( false false );
                }
                
return  encoding;
            }
        }

        
public   int  Level
        {
            
get  {  return  level; }
        }

        
public   string  Category
        {
            
get  {  return  category; }
        }
    }

}

调用方法

 

         ///   <summary>
        
///  判断用户名密码是否正确
        
///   </summary>
        
///   <param name="username"> 用户名 </param>
        
///   <param name="password"> 密码 </param>
        
///   <returns></returns>
         public   bool  Exists( string  username,  string  password)
        {
            
bool  isexists  =   false ;
            db.Log 
=   new  DebuggerWriter();
            
int  user  =  db.AdminUser.Count(p  =>  p.AUACCOUNT  ==  username  &&  p.AUPWD  ==  password);
            isexists 
=  user  !=   0   ?   true  :  false ;
            
return  isexists;
        }

 

转载于:https://www.cnblogs.com/yinpeng186/archive/2011/05/16/2047961.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值