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();

自定义log日志的更多相关文章

  1. JFinal - Log 日志

    今天偶然发现 JFinal 的 Log 简单小巧.上代码. JFinal 在初始化的时候有初始化 Log. class Config { // ... static void configJFinal ...

  2. (Unity)Unity自定义Debug日志文件,利用VS生成Dll文件并使用Dotfuscated进展混淆,避免被反编译

    Unity自定义Debug日志文件,利用VS生成Dll文件并使用Dotfuscated进行混淆,避免被反编译. 1.打开VS,博主所用版本是Visual Studio 2013. 2.新建一个VC项目 ...

  3. 第四十二篇、自定义Log打印

    1.在Xcode 8出来之后,需要我们去关闭多余的日志信息打印 2.在开发的过程中,打印调试日志是一项比不可少的工程,但是在iOS 10中NSLog打印日志被屏蔽了,就不得不使用自定义Log 3.去掉 ...

  4. java中关于log日志

    博:http://zhw2527.iteye.com/blog/1006302 http://zhw2527.iteye.com/blog/1099658 在项目开发中,记录错误日志是一个很有必要功能 ...

  5. 转 -Filebeat + Redis 管理 LOG日志实践

    Filebeat + Redis 管理 LOG日志实践 小赵营 关注 2019.01.06 17:52* 字数 1648 阅读 24评论 0喜欢 2 引用 转载 请注明出处 某早上,领导怒吼声远远传来 ...

  6. goaccess iis w3c 自定义log 格式参考

    goaccess 支持强大的自定义log 格式,比如我们需要分析iis w3c 格式日志 参考iis w3c 字段 date time s-ip cs-method cs-uri-stem cs-ur ...

  7. apparmor 引起自定义mysql 日志问题

    今天手贱,看到mysql 的日志在/var/log/mysql下面.总是觉得别扭,于是就想改变日志的位置, 本人开发环境 vagrant  + ubuntu12.04 ,在/etc/mysql/mys ...

  8. 简单的php自定义错误日志

    平时经常看php的错误日志,很少有机会去自己动手写日志,看了王健的<最佳日志实践>觉得写一个清晰明了,结构分明的日志还是非常有必要的. 在写日志前,我们问问自己:为什么我们有时要记录自定义 ...

  9. Log 日志工具类 保存到文件 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

随机推荐

  1. Xcode 8 : iOS xib is missing from working copy、iOS misssing file

    1,造成此问题可能是SVN的原因,通过命令行解决. 2.未使用SVN,竟然是icloud 造成的,通过 改变网络状态 + 重启Xcode 解决.

  2. BZOJ 2466: [中山市选2009]树

    Sol 树形DP. 听说有非常神奇的高斯消元的做法...orz... 然而我只会 \(O(n)\) 的树形DP. 首先一个点的状态只于他的父节点和子树有关,跟他 子树的子树 和 父亲的父亲 都没有任何 ...

  3. Java获取新浪微博cookies

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...

  4. Linux 查找指定名称的进程并显示进程详细信息

    实际应用中可能有这样的场景:给定一个进程名称特征串,查找所有匹配该进程名称的进程的详细信息. 解决的办法是: (1) 先用pgrep [str] 命令进行模糊匹配,找到匹配该特征串的进程ID: (2) ...

  5. Delete a node from BST

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  6. 【leetcode】Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...

  7. IDEA Error:java: 未结束的字符串文字

    首页 > 编程交流 > 基础篇 > IDEA Error:java: 未结束的字符串文字 201601-25 IDEA Error:java: 未结束的字符串文字   IDEA开发, ...

  8. jenkins结合ansible用shell实现自动化部署和回滚

    最近用jenkins+gitlab+ansible做持续化集成,自动化部署和版本回滚.然而deploy plugin没能做到增量升级和回滚操作,折腾了很久决定自己写个脚本来简单实现. 环境: cent ...

  9. iOS UILocalNotification 每2周,每两个月提醒

    iOS 的UILocalNotification提醒提供了默认的重复频率,比如,一天,一个星期等等,但是对于非标准的频率,比如每,2周,每2个月,无法重复提醒. 我们的思路是在应用程序开始时,把即将发 ...

  10. 压测 apache ab 初探

    2015年10月30日 14:58:34 ab是apache自带的压测命令, 在其bin目录下边, 不仅可以压测Apache, 也可以测nginx或其他服务器 可以模拟上传post值 (-p, 与下边 ...