Json转对象(忘记从哪个网址复制的了,侵删)
/// <summary>
/// 对象转为json
/// </summary>
/// <typeparam name="ObjType"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string ObjToJsonString<ObjType>(ObjType obj) where ObjType : class
{
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
string s = jsonSerializer.Serialize(obj);
return s;
}
/// <summary>
/// json转为对象
/// </summary>
/// <typeparam name="ObjType"></typeparam>
/// <param name="JsonString"></param>
/// <returns></returns>
public static ObjType JsonStringToObj<ObjType>(string JsonString) where ObjType : class
{
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
ObjType s = jsonSerializer.Deserialize<ObjType>(JsonString);
return s;
}
然后就是日志了这个网上一大堆一大堆的,然而这是我以前从中国开源下载的一个叫Brnmall商城的一个开源代码里发现的一个日志代码,感觉还不错,就用这个日志。
private static readonly object writeFile = new object();
public static void WriteLog(string debugstr)
{
lock (writeFile)//lock就是把{}中的代码段锁定,防止多个线程同时运行该代码段。
{
FileStream fs = null;
StreamWriter sw = null;
try
{
string filename = DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
//服务器中日志目录
string folder = HttpContext.Current.Server.MapPath("~/Log");
if (!Directory.Exists(folder)) //判断有没有文件夹,没有就创建
Directory.CreateDirectory(folder);
fs = new FileStream(folder + "/" + filename, System.IO.FileMode.Append, System.IO.FileAccess.Write);
sw = new StreamWriter(fs, Encoding.UTF8);
sw.WriteLine(DateTime.Now.ToString() + " " + debugstr + "\r\n");
}
finally
{
if (sw != null)
{
sw.Flush();
sw.Dispose();
sw = null;
}
if (fs != null)
{
// fs.Flush();
fs.Dispose();
fs = null;
}
}
}
}