我一直在寻找同样的方法,并拼凑了各种帖子的解决方案,并创建了一个小型控制台应用程序,以便在写入日志文件时输出实时查询文本 . 这在我的情况下很重要,因为我正在使用MySQL的实体框架,我需要能够检查生成的SQL .
创建日志文件的步骤(其他帖子的一些重复,为简单起见,所有这些都是):
编辑位于以下位置的文件:
C:\Program Files (x86)\MySQL\MySQL Server 5.5\my.ini
将“log = development.log”添加到文件的底部 . (注意保存此文件需要我以管理员身份运行我的文本编辑器) .
使用MySql工作台打开命令行,输入密码 .
运行以下命令以打开将记录所有运行的查询的常规日志记录:
SET GLOBAL general_log = 'ON';
To turn off:
SET GLOBAL general_log = 'OFF';
这将导致运行查询被写入以下位置的文本文件 .
C:\ProgramData\MySQL\MySQL Server 5.5\data\development.log
创建/运行一个控制台应用程序,它将实时输出日志信息:
资源:
using System;
using System.Configuration;
using System.IO;
using System.Threading;
namespace LiveLogs.ConsoleApp
{
class Program
{
static void Main(string[] args)
{
// Console sizing can cause exceptions if you are using a
// small monitor. Change as required.
Console.SetWindowSize(152, 58);
Console.BufferHeight = 1500;
string filePath = ConfigurationManager.AppSettings["MonitoredTextFilePath"];
Console.Title = string.Format("Live Logs {0}", filePath);
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
// Move to the end of the stream so we do not read in existing
// log text, only watch for new text.
fileStream.Position = fileStream.Length;
StreamReader streamReader;
// Commented lines are for duplicating the log output as it's written to
// allow verification via a diff that the contents are the same and all
// is being output.
// var fsWrite = new FileStream(@"C:\DuplicateFile.txt", FileMode.Create);
// var sw = new StreamWriter(fsWrite);
int rowNum = 0;
while (true)
{
streamReader = new StreamReader(fileStream);
string line;
string rowStr;
while (streamReader.Peek() != -1)
{
rowNum++;
line = streamReader.ReadLine();
rowStr = rowNum.ToString();
string output = String.Format("{0} {1}:\t{2}", rowStr.PadLeft(6, '0'), DateTime.Now.ToLongTimeString(), line);
Console.WriteLine(output);
// sw.WriteLine(output);
}
// sw.Flush();
Thread.Sleep(500);
}
}
}
}