人世间最痛苦的事是人死了钱没花了,程序员最悲哀的事是人走了BUG没处理好。所以依小弟拙见程序的健壮性尤为重要,那么针对此问题,小弟无才,不能全面刨析程序运行时的怪异(一般也就是像小弟这样的,代码写的太畸形,导致程序怪异)。为了解决畸形代码,鼓励踹程序,踹了之后记录下来,方便我们日后分析,逐渐提升自身代码规范安全。
代码如下:

LogManager
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Text;
using System.IO;
namespace Paul.Log
{
#region 日志类型枚举
public enum LogType
{
未定义, 操作, 异常
}
#endregion
///
/// 日志管理
///
public class LogManager
{
#region 私有字段
private Guid _guid; //日志标识
private DateTime _createDateTime; //日志创建日期
private string _logName; //日志名称
private string _content; //日志内容
private LogType _logType; //日志类型
private string _path; //日志完整路径
#endregion
#region 公有属性
public Guid Guid
{
get
{
if (_guid == null)
{
_guid = Guid.NewGuid();
}
return _guid;
}
set { _guid = value; }
}
public DateTime CreateDateTime
{
get
{
if (_createDateTime == null)
{
_createDateTime = DateTime.Now;
}
return _createDateTime;
}
set { _createDateTime = value; }
}
public string LogName
{
get { return _logName; }
set { _logName = value; }
}
public string Content
{
get { return _content; }
set { _content = value; }
}
public LogType LogType
{
get { return _logType; }
set { _logType = value; }
}
public string Path
{
get { return _path; }
set { _path = value; }
}
#endregion
#region 构造方法
///
/// 创建日志对象
///
///
///
///
///
///
///
public LogManager(Guid guid, DateTime createDateTime, string logName, string content, LogType logType, string _path)
{
this._guid = guid;
this._createDateTime = createDateTime;
this._logName = logName;
this._content = content;
this._logType = logType;
this._path = Path;
}
public LogManager(string content, LogType logType, string path)
{
this._content = content;
this._logType = logType;
this.Path = path;
}
public LogManager(string content, LogType logType)
{
this._content = content;
this._logType = logType;
}
public LogManager() { }
#endregion
#region 公有方法
///
/// 创建新日志文件并写入日志内容
///
public void CreateLogFileAndWriteContent()
{
//获取文件后缀名
string suffix = this._path.Substring(this._path.LastIndexOf('.') + 1, 3);
if (Directory.Exists(_path) == false)
{
try
{
Directory.CreateDirectory(_path.Substring(0, this._path.LastIndexOf('/')));
}
catch (Exception)
{
throw new Exception("文件路径错误!");
}
}
if (suffix != "txt")
{
throw new Exception("请指定完整路径(包括文件后缀名)!");
}
else if (File.Exists(this._path))
{
throw new Exception("该日志文件名已经存在,请尝试调用追加方法");
}
else
{
FileStream fs;
fs = new FileStream(this._path, FileMode.Create);
WriteLogMethod(fs);
fs.Close();
}
}
///
/// 追加日志
///
public void Appread()
{
try
{
AppendLog();
}
catch (Exception)
{
throw new Exception("追加异常,检查变量是否正常");
}
}
#endregion
#region 私有方法
///
/// 写入新日志
///
///
private void WriteLogMethod(FileStream fs)
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine("******************************************************");
sw.WriteLine(string.Format("日志标识:{0}", this._guid));
sw.WriteLine(string.Format("日志类型:{0}", this._logType));
sw.WriteLine(string.Format("日志名称:{0}", this._logName));
sw.WriteLine(string.Format("日志日期:{0}", this._createDateTime));
sw.WriteLine("------------------------------------------------------");
sw.WriteLine(string.Format("日志内容:{0}", this._content));
sw.WriteLine("------------------------------------------------------");
sw.WriteLine("******************************************************");
sw.Close();
}
}
///
/// 追加日志
///
private void AppendLog()
{
using (StreamWriter sw = File.AppendText(this._path))
{
sw.WriteLine(string.Format("日志标识:{0}", this._guid));
sw.WriteLine(string.Format("日志类型:{0}", this._logType));
sw.WriteLine(string.Format("日志名称:{0}", this._logName));
sw.WriteLine(string.Format("日志日期:{0}", this._createDateTime));
sw.WriteLine("------------------------------------------------------");
sw.WriteLine(string.Format("日志内容:{0}", this._content));
sw.WriteLine("------------------------------------------------------");
sw.WriteLine("******************************************************");
sw.Close();
}
}
#endregion
}
}
测试如下:

测试代码
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LogTest
{
class Test
{
static void Main()
{
Paul.Log.LogManager lm = new Paul.Log.LogManager();
try
{
string s = null;
s.ToString();
}
catch (Exception ex)
{
lm.Guid = Guid.NewGuid();
lm.CreateDateTime = DateTime.Now;
lm.LogName = ex.Message;
lm.LogType = Paul.Log.LogType.异常;
lm.Path = "Log/error.txt";
lm.Content = ex.StackTrace;
lm.Appread();
}
}
}
}
还望不吝赐教,指出BUG,小弟修正。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-659727/ ,如需转载,请注明出处,否则将追究法律责任。