帮助类的内容如下:
public class FileWrite
{
public static void WriteJson(string file, string value)
{
var time = "\r\n" + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "\r\n";
using (var fs = new FileStream(file, FileMode.Append))
{
var sw = new StreamWriter(fs);
sw.Write(time + value);
sw.Close();
}
}
public static void WriteJson(string file, object value)
{
var json = JsonConvert.SerializeObject(value);
WriteJson(file, json);
}
}
调用示例:
传参 :字符串
/// <summary>
/// 方法抛出异常,将异常信息写入文件
/// </summary>
/// <param name="message"></param>
/// <param name="fileName"></param>
private void WriteException(string message, string fileName)
{
var file = Path.Combine(_boTestFile, fileName);
FileWrite.WriteJson(file, message);
}
传参:对象类型
private void WriteInfo(object person, string fileName)
{
string _boTestFile = Environment.CurrentDirectory + "\\BOService";
var file = Path.Combine(_boTestFile, fileName);
FileWrite.WriteJson(file, person);
}