现在的位置: 首页 > 综合 > 正文

写日志文件类

2012年03月10日 ⁄ 综合 ⁄ 共 5338字 ⁄ 字号 评论关闭
IAppLog.cs
 using System;

namespace Bx.Web.Common.FileLog
{
    
/// <summary>
    
/// Summary description for IAppLog.
    
/// </summary>

    public interface IAppLog
    
{
        
string GetName();
        
bool Clear();
        
bool LineFeed();
        
bool Write( string strBuffer );
        
bool WriteEvent( string strEvent, int nType, bool bLineFeed );
        
bool WriteInformation( string strEvent );
        
bool WriteWarning( string strEvent );
        
bool WriteError( string strEvent );
        
bool WriteError( string strWhere, string strEvent );
    }

}

AppLog.cs

using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Configuration;
using System.Reflection;

namespace Bx.Web.Common.FileLog
{
    
/// <summary>
    
/// 
    
/// </summary>

    public class AppLog : IAppLog
    
{
        
public AppLog()
        
{
            
// 
            
// TODO: Add constructor logic here
            
//
        }


        
public string GetName()
        
{
            
return "";
        }


        
public bool Clear()
        
{
            
return true;
        }


        
public bool LineFeed()
        
{
            
return true;
        }


        
public bool Write( string strBuffer )
        
{
            
return true;
        }


        
public bool WriteEvent( string strEvent, int nType, bool bLineFeed )
        
{
            
//拼成字符串
            const string strNull = "<NULL>";
            StringBuilder sbEvent 
= new StringBuilder();

            sbEvent.Append( 
'\x1b' );

            
if( nType == (int)EventLogEntryType.Warning )
                sbEvent.Append( 
"" );
            
else if( nType == (int)EventLogEntryType.Error )
                sbEvent.Append( 
"" );
            
else
                sbEvent.Append( 
"" );

            
//获取时间
            DateTime dtCur = DateTime.Now;
            
string strCurTime = dtCur.ToString( "yyyyMMdd HHmmss" );
            sbEvent.Append( strCurTime );

            
//填入事件字符串
            if( strEvent == null || strEvent.Length == 0 )
            
{
                sbEvent.Append( strNull );
            }

            
else
            
{
                sbEvent.Append( strEvent );
            }


            
//换行 & 回车
            if( bLineFeed ) sbEvent.Append( "\x0d\x0a" );

            
return do_Write( sbEvent.ToString() );
        }


        
public bool WriteInformation( string strEvent )
        
{
            
return WriteEvent( strEvent, (int)EventLogEntryType.Information, true );
        }


        
public bool WriteWarning( string strEvent )
        
{
            
return WriteEvent( strEvent, (int)EventLogEntryType.Warning, true );
        }


        
public bool WriteError( string strEvent )
        
{
            
return WriteEvent( strEvent, (int)EventLogEntryType.Error, true );
        }


        
public bool WriteError( string strWhere, string strEvent )
        
{
            StringBuilder sbMsg 
= new StringBuilder();
            sbMsg.Append( strWhere );
            sbMsg.Append( 
" " );
            sbMsg.Append( strEvent );

            
return WriteEvent( sbMsg.ToString(), (int)EventLogEntryType.Error, true );
        }


        
//负责写到文件里面
        protected bool do_Write( string strEvent )
        
{
            
try
            
{
                
//获取文件名
                string strTmp = System.Configuration.ConfigurationSettings.AppSettings["LogFileFullName"];
                
string strFileName = String.Format(strTmp, DateTime.Now.ToString("yyyyMMdd"));
                
                
if!File.Exists( strFileName ) )
                
{
                    System.IO.Directory.CreateDirectory( 
new System.IO.FileInfo( strFileName).DirectoryName );    //建立目录结构。
                }

                
                
//写文件
            
                StreamWriter swFile 
= File.AppendText( strFileName );
                swFile.Write( strEvent );
                swFile.Close();
                
return true;
            }

            
catch( Exception ex )
            
{
                Debug.WriteLine( ex.ToString() );
                
return false;
            }

        }


        
public static string GetFields(object obj)
        
{
            Type currentType 
= obj.GetType();

            FieldInfo[] fields;
            
//string fieldName;
            string fieldStr = string.Empty;

            fields 
= currentType.GetFields(
                    BindingFlags.NonPublic 
|
                    BindingFlags.Instance 
|
                    BindingFlags.Public);
            
foreach (FieldInfo field in fields)
            
{
                
object value = field.GetValue(obj);
                
string strPropertyName = field.Name;

                
if (value != null && value.ToString() != string.Empty)
                
{
                    
if (fieldStr != null && fieldStr.ToString() != string.Empty)
                    
{
                        fieldStr 
+= "" + strPropertyName + "=" + value.ToString();
                    }

                    
else
                    
{
                        fieldStr 
+= strPropertyName + "=" + value.ToString();
                    }

                }

            }

            
return fieldStr;
        }

    }

}

WebLog.cs

using System;
using System.Collections.Generic;
using System.Text;

using Bx.Web.Common.FileLog;

namespace Bx.Web.Common
{
    
public class WebLog
    
{
        
private static AppLog appLog = new AppLog();


        
/// <summary>
        
/// 获取网站Log文件
        
/// </summary>

        public static IAppLog LogFile
        
{
            
get

抱歉!评论已关闭.