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. JDBC MySQL

    JDBC连接MySQL 加载及注册JDBC驱动程序 Class.forName("com.mysql.jdbc.Driver"); Class.forName("com. ...

  2. git 教程(4)--版本回退

    现在,你已经学会了修改文件,然后把修改提交到Git版本库,现在,再练习一次,修改readme.txt文件如下: Git is a distributed version control system. ...

  3. &,引用复制@,忽略错误提示

    function &chhua() { static $b="www.jb51.net";//申明一个静态变量 $b=$b."WEB开发"; echo ...

  4. String封装——读时共享,写时复制

    碰到过一位一直怀疑C++标准库(STL)效率的人,他说STL效率太低,企业开发根本不会用.我是持反对意见的. 说这话的人,肯定没有做过大量的调查.没有调查就没有发言权. STL的效率是不低的,足够满足 ...

  5. Java解析采集模块

    package step3; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; im ...

  6. django缓存

    由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中,5 ...

  7. 修改vb程序图标

    1. 2.

  8. C# 复制(深拷贝、浅拷贝)

    Object.MemberwiseClone 方法 创建当前 Object 的浅表副本. protected Object MemberwiseClone() MemberwiseClone 方法创建 ...

  9. python操作Excel文件

    参考: http://www.cnblogs.com/tianyajuanke/p/4048844.html http://blog.chinaunix.net/uid-24701781-id-334 ...

  10. centos7.0 安装LNMP运行环境

    LNMP作为php流行的运行环境,而最近需要搭建一个内部的php论坛.记录下LNMP的安装: 1.安装mysql 请参考:centos7 安装mysql5.7.11注意事项 2.安装php yum i ...