Log.cs (这个已经不能用了,用下面的问题解决方案

using System;
using System.Collections.Generic;
using System.Web;
using System.IO; namespace PC.Common
{
public class Log
{
//在网站根目录下创建日志目录
public static string path = HttpContext.Current.Request.PhysicalApplicationPath + "logs"; /**
* 向日志文件写入调试信息
* @param className 类名
* @param content 写入内容
*/
public static void Debug(string className, string content)
{
if (LogLevel.LOG_LEVENL >= )
{
WriteLog("DEBUG", className, content);
}
} /**
* 向日志文件写入运行时信息
* @param className 类名
* @param content 写入内容
*/
public static void Info(string className, string content)
{
if (LogLevel.LOG_LEVENL >= )
{
WriteLog("INFO", className, content);
}
} /**
* 向日志文件写入出错信息
* @param className 类名
* @param content 写入内容
*/
public static void Error(string className, string content)
{
if (LogLevel.LOG_LEVENL >= )
{
WriteLog("ERROR", className, content);
}
} /**
* 实际的写日志操作
* @param type 日志记录类型
* @param className 类名
* @param content 写入内容
*/
protected static void WriteLog(string type, string className, string content)
{
if (!Directory.Exists(path))//如果日志目录不存在就创建
{
Directory.CreateDirectory(path);
} string time = DateTime.Now.ToString("HH:mm:ss.fff");//获取当前系统时间
string filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名 //创建或打开日志文件,向日志文件末尾追加记录
StreamWriter mySw = File.AppendText(filename); DateTime now = DateTime.Now; if (now.Hour > && now.Hour < )
{
//向日志文件写入内容
string write_content = "「凌晨」" + time + " " + type + " " + className + ": " + content;
mySw.WriteLine(write_content);
}
else if (now.Hour > && now.Hour < )
{
//向日志文件写入内容
string write_content = "【上午】" + time + " " + type + " " + className + ": " + content;
mySw.WriteLine(write_content);
}
else if (now.Hour > && now.Hour < )
{
//向日志文件写入内容
string write_content = "『下午』" + time + " " + type + " " + className + ": " + content;
mySw.WriteLine(write_content);
}
else if (now.Hour > && now.Hour < )
{
//向日志文件写入内容
string write_content = "〖晚上〗" + time + " " + type + " " + className + ": " + content;
mySw.WriteLine(write_content);
} //关闭日志文件
mySw.Close();
}
}
}

LogLevel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace PC.Common
{
public class LogLevel
{
public static string AppKey(string key)
{
return System.Configuration.ConfigurationManager.AppSettings[key];
} /// <summary>
/// 日志等级,0.不输出日志;1.只输出错误信息; 2.输出错误和正常信息; 3.输出错误信息、正常信息和调试信息
/// </summary> public static int LOG_LEVENL
{
get
{
string log_levenl = "";
if (AppKey("log_leven") != "")
{
log_levenl = AppKey("log_leven");
}
return Convert.ToInt32(log_levenl);
}
}
}
}

web.config

调用方式

Log.Debug(this.GetType().ToString(), "json : " + json);

问题:正由另一进程使用,因此该进程无法访问该文件

log.cs 更改

using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Text; namespace PC.Common
{
public class Log
{
//在网站根目录下创建日志目录
//public static string path = HttpContext.Current.Request.PhysicalApplicationPath + "logs";
public static string path = System.AppDomain.CurrentDomain.BaseDirectory + "logs"; /**
* 向日志文件写入调试信息
* @param className 类名
* @param content 写入内容
* @param remark 备注
*/
public static void MostDebug(string className, string content, string remark)
{
if (LogLevel.LOG_LEVENL >= )
{
WriteLog("MostDebug", className, content, remark);
}
} /**
* 向日志文件写入调试信息
* @param className 类名
* @param content 写入内容
* @param remark 备注
*/
public static void Debug(string className, string content, string remark)
{
if (LogLevel.LOG_LEVENL >= )
{
WriteLog("DEBUG", className, content, remark);
}
} /**
* 向日志文件写入运行时信息
* @param className 类名
* @param content 写入内容
* @param remark 备注
*/
public static void Info(string className, string content, string remark)
{
if (LogLevel.LOG_LEVENL >= )
{
WriteLog("INFO", className, content, remark);
}
} /**
* 向日志文件写入出错信息
* @param className 类名
* @param content 写入内容
* @param remark 备注
*/
public static void Error(string className, string content, string remark)
{
if (LogLevel.LOG_LEVENL >= )
{
WriteLog("ERROR", className, content, remark);
}
} /**
* 实际的写日志操作
* @param type 日志记录类型
* @param className 类名
* @param content 写入内容
* @param remark 备注
*/
protected static void WriteLog(string type, string className, string content, string remark)
{
//用户浏览器标识
//string agent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"] == null ? "后端调用" : HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"].ToString(); if (!Directory.Exists(path))//如果日志目录不存在就创建
{
Directory.CreateDirectory(path);
} string time = DateTime.Now.ToString("HH:mm:ss.fff");//获取当前系统时间
string filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名 if (!File.Exists(filename))
{
File.Create(filename).Close();
} //解决【正由另一进程使用,因此该进程无法访问该文件】
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{ DateTime now = DateTime.Now; string write_content = "[" + time + "]【" + type + "】" + remark + " (" + className + "): " + content;//+ "(" + agent + ")";
byte[] bytes = null;
if (now.Hour >= && now.Hour < )
{
//向日志文件写入内容
write_content = "「凌晨」" + write_content;
bytes = Encoding.Default.GetBytes(write_content);
}
else if (now.Hour >= && now.Hour < )
{
//向日志文件写入内容
write_content = "【上午】" + write_content;
bytes = Encoding.Default.GetBytes(write_content);
}
else if (now.Hour >= && now.Hour < )
{
//向日志文件写入内容
write_content = "『下午』" + write_content;
bytes = Encoding.Default.GetBytes(write_content);
}
else if (now.Hour >= && now.Hour < )
{
//向日志文件写入内容
write_content = "〖晚上〗" + write_content;
bytes = Encoding.Default.GetBytes(write_content);
}
//2、写操作
fs.Position = fs.Length;
fs.Write(bytes, , bytes.Length);
//byte(13) byte(10)等效于 \r\n,直接输入\r\n不起作用
fs.WriteByte();
fs.WriteByte();
fs.Flush();//清空流
} }
}
} ////创建或打开日志文件,向日志文件末尾追加记录
//StreamWriter mySw = File.AppendText(filename); //DateTime now = DateTime.Now; //string write_content = "[" + time + "]【" + type + "】" + remark + " (" + className + "): " + content;//+ "(" + agent + ")"; //if (now.Hour >= 0 && now.Hour < 8)
//{
// //向日志文件写入内容
// write_content = "「凌晨」" + write_content;
// mySw.WriteLine(write_content);
//}
//else if (now.Hour >= 8 && now.Hour < 12)
//{
// //向日志文件写入内容
// write_content = "【上午】" + write_content;
// mySw.WriteLine(write_content);
//}
//else if (now.Hour >= 12 && now.Hour < 18)
//{
// //向日志文件写入内容
// write_content = "『下午』" + write_content;
// mySw.WriteLine(write_content);
//}
//else if (now.Hour >= 18 && now.Hour < 24)
//{
// //向日志文件写入内容
// write_content = "〖晚上〗" + write_content;
// mySw.WriteLine(write_content);
//} ////关闭日志文件
//mySw.Close();

最新文章

  1. 1_UILabel
  2. android opengl
  3. javascript之DOM篇一
  4. unity, SerializedObject.FindProperty不要写在Editor的OnEnable里,要写在OnInspectorGUI里
  5. printf的格式控制的完整格式
  6. mssql的delete用用到被delete的表的别名
  7. bzoj 1432 [ZJOI2009]Function(找规律)
  8. 什么是JSON?如何使用?它比BSON更好吗?
  9. 学习Swift--枚举的初步认识 --个人备忘 大神勿喷
  10. setAnimationStyle实现的popwindow显示消失的动画效果
  11. 用CATransform3D实现3D效果和制作简单3D动画
  12. Swift中枚举的总结以及使用
  13. 总结&#183;CSS3中定位模型之position属性的使用方法
  14. jdk安装路径
  15. Auzre系列1.1.1 —— 安装用于 IntelliJ 的 Azure 工具包
  16. c++ 日志输出库 spdlog 简介(1)
  17. springboot系列十二、springboot集成RestTemplate及常见用法
  18. Oracle数据库入门——体系结构
  19. 20170814xlVBA PowerPoint分类插图加说明
  20. thrift 知识点

热门文章

  1. 获取action name在asp.net mvc
  2. Laravel5.1-Eloquent ORM:起步
  3. Kali Linux渗透基础知识整理(一):信息搜集
  4. linux下一个有意思的问题(文件名以短划线或空格开头)
  5. iframe自适应宽度
  6. django ATOMIC_REQUESTS
  7. Silverlight datagrid 排序 (转)
  8. Windows Setup progject : 修改默认安装路径
  9. spring + myBatis 常见错误:@Autowired注解失败
  10. C++ 中超类化和子类化