惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
Jina AI
Jina AI
H
Help Net Security
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
L
LangChain Blog
Recorded Future
Recorded Future
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
GbyAI
GbyAI
B
Blog RSS Feed
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
M
MIT News - Artificial intelligence
爱范儿
爱范儿
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
B
Blog
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
MongoDB | Blog
MongoDB | Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
The Cloudflare Blog
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
D
DataBreaches.Net
博客园 - 【当耐特】
T
Troy Hunt's Blog
V
Visual Studio Blog
V2EX - 技术
V2EX - 技术
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google Online Security Blog
Google Online Security Blog
The GitHub Blog
The GitHub Blog

博客园 - 朱小能

C#对Dictionary的按Value排序 linq to sql中的自动缓存(对象跟踪) javascript 调用webservice 的几种方法 修改域名映射IP地址 SQL 语句,类型转换 hyper-v 网络连接 VS2010 定位文件在solution中的位置 oracle连接字符串配置 spool的简单使用 open数据库Timeout expired 错误 文件的还原与备份 - 朱小能 - 博客园 ASCII码,对应e.KeyChar C# 调用计算器,日历 - 朱小能 - 博客园 Excel 如何由一个文本算术公式得到一个结果 Word、Excel中输入当前日期及时间的快捷键 c# ToString 格式化数字 - 朱小能 GridView绑定 获取access中表的相关信息 List<T> 排序 - 朱小能 - 博客园
Filelog - 朱小能 - 博客园
朱小能 · 2009-06-30 · via 博客园 - 朱小能

做了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);