常用方法:

public class FileHelper
{
private static void Write(string fileName, byte[] bytes)
{
FileStream fs = null;
string filePath = "C:\\temp\\" + fileName + ".ecg";
Encoding encoder = Encoding.UTF8;
try
{ //判断文件是否存在
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
fs = File.OpenWrite(filePath);
fs.Position = fs.Length;
fs.Write(bytes, 0, bytes.Length);
}
catch (Exception ex)
{
NLogHelper.Error(ex.Message);
}
finally
{
if (fs != null)
fs.Close();
}
} public static void Write(string msg)
{
System.IO.FileStream fs = null;
System.IO.StreamWriter sw = null;
try
{
string time = System.DateTime.Now.ToString();
var fileName = "C:\\temp\\vipsoft.log";
string filePath = System.Web.HttpContext.Current.Server.MapPath(fileName);
fs = new System.IO.FileStream(filePath, System.IO.FileMode.Append);
sw = new System.IO.StreamWriter(fs, System.Text.Encoding.GetEncoding("utf-8"));
sw.WriteLine("======================");
sw.WriteLine(time);
sw.WriteLine("----------------------");
sw.WriteLine(msg);
sw.WriteLine("----------------------");
}
catch (Exception e)
{
throw e;
}
finally
{
if (fs != null)
{
fs.Close();
}
if (sw != null)
{
sw.Close();
}
}
} /// <summary>
/// 用得比较多
/// </summary>
/// <param name="msg"></param>
public static void WriteAppend(string msg)
{
System.IO.StreamWriter sw = null;
try
{
var fileName = "C:\\temp\\vipsoft.log";
//判断文件是否存在
if (!Directory.Exists(fileName))
{
Directory.CreateDirectory(fileName);
}
sw = File.AppendText(fileName);
sw.WriteLine(msg);
sw.Flush();
sw.Close();
}
catch (Exception e)
{
throw e;
}
finally
{
if (sw != null)
{
sw.Close();
}
} } }

C# 写日志文件的更多相关文章

  1. [转]ASP.NET Core 开发-Logging 使用NLog 写日志文件

    本文转自:http://www.cnblogs.com/Leo_wl/p/5561812.html ASP.NET Core 开发-Logging 使用NLog 写日志文件. NLog 可以适用于 . ...

  2. ASP.NET Core 开发-Logging 使用NLog 写日志文件

    ASP.NET Core 开发-Logging 使用NLog 写日志文件. NLog 可以适用于 .NET Core 和 ASP.NET Core . ASP.NET Core已经内置了日志支持,可以 ...

  3. Log4j写日志文件使用详解

    Log4j输出到控制台成功,写入文件失败 - Log4j和commons log的整合 一.今天在使用commongs-logging.jar和log4j.properties来输出系统日志的时候,发 ...

  4. .net core 2.0使用NLog写日志文件

    原文地址:传送门 之前也看了 linezero 大佬写的教程,但是总是没有成功写入日志文件.按照 曲廉卿 的已成功,以下正文: 最近研究了一下NLog的使用方式,简单的入了一下门. 实现的功能,对于不 ...

  5. 为何要打印日志?C++在高并发下如何写日志文件(附源码)?

    为何要打印日志?让程序裸奔不是一件很快乐的事么? 有些BUG就像薛定谔的猫,具有波粒二象性,当你试图去观察它时它就消失了,当你不去观察它时,它又会出现.当你在测试人员面前赌咒发誓,亲自路演把程序跑一遍 ...

  6. kettle作业中的js如何写日志文件

    在kettle作业中JavaScript脚本有时候也扮演非常重要的角色,此时我们希望有一些日志记录.下面是job中JavaScript记录日志的方式. job的js写日志的方法. 得到日志输出实例 o ...

  7. php服务端写日志文件

    1.需求 在服务端记录日志 2.基础版 最基础的文件读写,(要注意window和linux的换行符,window是\r\n,linux是\n),这里就写入一个时间. <?php $handle ...

  8. 修改winform安装包写日志文件权限

    1.如果程序有写入文件的功能要添加该文件 并配置该属性 改成这个即可

  9. 一种多线程写日志文件的解决方案 c#源代码演示

    using System;using System.Collections.Generic;using System.Collections;using System.Text;using Syste ...

  10. vb.net 写日志文件

    Module mod_LogAccessHt #Region "Const" Public Const __PREFIX_ERROR__ As String = "Err ...

随机推荐

  1. CSS必学:元素之间的空白与行内块的幽灵空白问题

    作者:WangMin 格言:努力做好自己喜欢的每一件事 CSDN原创文章 博客地址 WangMin 我们在开发的过程中,难免会出现一些难以预料的问题.那么其中,CSS空白现象就是非常常见的问题之一.虽 ...

  2. git中的ole mode 和 new mode提示问题

    git status 显示如下(文件内容其实并没有改变): old mode 100644 new mode 100755 原因是: 使用chmod修改过文件权限后,filemode会有变化. 解决办 ...

  3. Educational Codeforces Round 104 (Rated for Div. 2) A~E题解

    写在前边 链接:Educational Codeforces Round 104 (Rated for Div. 2) A. Arena 链接:A题链接 题目大意: 给定一个长度为\(n\)的数组,表 ...

  4. Redmi AC2100 路由器 官方固件允许IPv6外网访问下游设备

    升级/降级 至 官方固件版本: 2.0.23 稳定版.操作入口在路由器常用设置-系统状态-升级检测处. 开启SSH权限.F12打开浏览器的开发者模式,并切换至终端选项卡,复制以下代码至终端处,并敲回车 ...

  5. RocksDB 在 vivo 消息推送系统中的实践

    作者:vivo 互联网服务器团队 - Zeng Luobin 本文主要介绍了 RocksDB 的基础原理,并阐述了 RocksDB 在vivo消息推送系统中的一些实践,通过分享一些对 RocksDB ...

  6. CentOS(7.6)环境下迁移Mysql(5.7)的data目录到指定位置

    第一步:关闭Mysql #关闭Mysql服务systemctl stop mysqld#查看Mysql服务状态 ps -ef|grep mysql 第二步:创建新目录,并拷贝数据文件 #创建data文 ...

  7. 如何编写一个 Pulsar Broker Interceptor 插件

    背景 之前写过一篇文章 VictoriaLogs:一款超低占用的 ElasticSearch 替代方案讲到了我们使用 Victorialogs 来存储 Pulsar 消息队列的消息 trace 信息. ...

  8. [ABC279G] At Most 2 Colors

    Problem Statement There is a grid with $1 \times N$ squares, numbered $1,2,\dots,N$ from left to rig ...

  9. C# 常量 结构体 委托

    常量 const double PI = 3.1415926; 常量名命名一般使用大写字母 枚举类型 开发一个游戏,游戏角色有法师(Mage).射手(Archer).刺客(Assassin).坦克(T ...

  10. 429 You are being rate limited

    记录贴 429 真的很让人伤心 清除浏览器数据 我用的 Edge : 设置 ⇒ 隐私.搜索和服务 ⇒ 清除浏览器数据 ⇒ 立即清除 然后就重新登陆可以了