I BELIEVE I CAN

加油,努力,奋斗!!

导航

Filelog

做了3年开发了,最近发现基础还是很重要,应该是深博,而不是博深,陆续贴点基础类

 

记录日志 

 


using System;
using System.IO;
using System.Text;

namespace TestForLogfile
{
    
public class LogFile
    {
        
//private static readonly string LogFileName = ConfigurationManager.AppSettings["LogFile"];
        private static readonly string LogFileName = "TestLogFile";

        
public static void LogInfo(string sLogInfo, LogType type)
        {
            
try
            {
                
string logLevelType = GetStringLogType(type);

                var sb 
= new StringBuilder();
                sb.Append(DateTime.Now.ToString(
"MM/dd HH:mm:ss "));
                sb.Append(
"  ");
                sb.Append(logLevelType);
                sb.Append(
"  ");
                sb.Append(sLogInfo);

                var filePathAndName 
=
                    
string.Format("{0}AppLog\\{1}.txt", AppDomain.CurrentDomain.BaseDirectory, LogFileName);

                
//write to file TecNodeService.log
                using (var fs = File.Open(filePathAndName, FileMode.Append, FileAccess.Write, FileShare.Write))
                {
                    var logFileSW 
= new StreamWriter(fs);
                    logFileSW.WriteLine(sb.ToString());
                    logFileSW.Close();
                    fs.Close();
                }

            }
            
catch (Exception ex)
            {
                
//write to file TecNodeServiceErr.txt
                string errFilePathAndName = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, LogFileName);

                
try
                {
                    var fs 
= File.Open(errFilePathAndName, FileMode.Append, FileAccess.Write, FileShare.Write);
                    var logFileSW 
= new StreamWriter(fs);
                    logFileSW.WriteLine(ex.Message);
                    logFileSW.Close();
                    fs.Close();
                }
                
catch
                {
                }
            }
        }

        
private static string GetStringLogType(LogType type)
        {
            
string logLevelType;

            
switch (type)
            {
                
case LogType.Trace:
                    {
                        logLevelType 
= "Trace";
                        
break;
                    }
                
case LogType.Info:
                    {
                        logLevelType 
= "Info";
                        
break;
                    }
                
case LogType.Warning:
                    {
                        logLevelType 
= "Warning";
                        
break;
                    }
                
case LogType.Error:
                    {
                        logLevelType 
= "Error";
                        
break;
                    }
                
case LogType.Start:
                    {
                        logLevelType 
= "Start";
                        
break;
                    }
                
case LogType.Stop:
                    {
                        logLevelType 
= "Stop";
                        
break;
                    }
                
default:
                    {
                        logLevelType 
= "Unknown";
                        
break;
                    }
            }
            
return logLevelType;
        }

        
public static void RenameLogFile(int numCopy)
        {
            
//find the location of log file
            string baseDirPath = AppDomain.CurrentDomain.BaseDirectory;
            
string logPath = baseDirPath + "AppLog";
            
if (!Directory.Exists(logPath))
            {
                
// Create log file
                Directory.CreateDirectory(logPath);
            }

            
// logfile path
            string logFile = string.Format("{0}AppLog\\{1}.txt", baseDirPath, LogFileName);
            FileInfo logFileInfo 
= new FileInfo(logFile);
            
if (logFileInfo.Exists)
            {
                
// recursive rename
                RecursiveRenameFile(logFile, numCopy);
                logFileInfo.Delete();
            }
        }

        
private static void RecursiveRenameFile(string fullName, int numCopy)
        {
            var logFileInfo 
= new FileInfo(fullName);
            
try
            {
                
if (logFileInfo.Exists)
                {
                    
//For example:  TestLogFile2.txt
                    int iLastDotPos = fullName.LastIndexOf('.');  //13
                    int iFileNamePos = fullName.LastIndexOf(LogFileName); //0
                    int iStartNumPos = iFileNamePos + LogFileName.Length;
                    
string nextFilePathAndName;
                    
int iSeqNum;
                    
if (iStartNumPos == iLastDotPos)
                    {
                        iSeqNum 
= 1;
                        nextFilePathAndName 
= fullName.Substring(0, iStartNumPos) + iSeqNum + ".txt";
                    }
                    
else
                    {
                        iSeqNum 
= Int32.Parse(fullName.Substring(iStartNumPos, iLastDotPos - iStartNumPos)) + 1;
                        nextFilePathAndName 
= fullName.Substring(0, iStartNumPos) + iSeqNum + ".txt";
                    }

                    
if (iSeqNum < numCopy)
                    {
                        RecursiveRenameFile(nextFilePathAndName, numCopy);
                    }
                    logFileInfo.CopyTo(nextFilePathAndName, 
true);
                }
            }
            
catch (Exception ex)
            {
                LogInfo(ex.Message, LogType.Error);
            }
        }
    }


    
public enum LogType
    {
        Trace 
= 0,
        Info,
        Warning,
        Error,
        Start,
        Stop,
        Unknown
    }
}

 

调用很简单

//9表示保留9个日志文件,循环向后拷贝
LogFile.RenameLogFile(9);

// 记录日志内容
LogFile.LogInfo("test", LogType.Error);

 

posted on 2009-06-30 14:45  朱小能  阅读(523)  评论(0编辑  收藏  举报