110,476
社区成员
发帖
与我相关
我的任务
分享
Module Module1
' 测试输出文件, 这个名称后面加 .copy 是测试输出文件的副本(由 Read 过程写入)
Private _File As String = "f:\temp\test.txt"
Private _Work As Boolean = False
Sub Main()
' 删除测试文件
If IO.File.Exists(_File) Then IO.File.Delete(_File)
' 通过线程启动工具输出文件的坊和写入副本
Dim iThread As New Threading.Thread(AddressOf Read)
iThread.Start()
' 启动 BCP 工具做文件导出
Using ps As New Process
With ps.StartInfo
.UseShellExecute = False
.FileName = "bcp"
.Arguments = """select top (1000) O.* from sys.all_objects O, sys.all_objects O1"" queryout f:\temp\test.txt -T -N"
End With
Console.WriteLine("Process Enger key to start")
Console.ReadLine()
_Work = True
If ps.Start() Then ps.WaitForExit()
ps.Close()
_Work = False
End Using
Console.WriteLine("Done !!! Process Enger key to Exit")
Console.ReadLine()
End Sub
Sub Read()
' 等待工作开始
While _Work = False
Threading.Thread.Sleep(1)
End While
' 等待文件就绪
While IO.File.Exists(_File) = False
Threading.Thread.Sleep(1)
End While
' 文件读写 buffer
Dim BufferSize = 1024 * 4
Dim Buffer(BufferSize) As Char
'工具写入文件的同步副本
Dim FileW = _File & ".copy"
If IO.File.Exists(FileW) Then IO.File.Delete(FileW)
Using fw As New IO.StreamWriter(FileW)
fw.AutoFlush = True
' 读取工具写入文件,并写入副本
Using fs As New IO.FileStream(_File, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.ReadWrite)
Using fr As New IO.StreamReader(fs)
Dim ReadCount As Integer = 0
' 工具工作中的持续读取
While _Work
ReadCount = fr.ReadBlock(Buffer, 0, BufferSize)
If ReadCount > 0 Then fw.Write(Buffer, 0, ReadCount)
End While
' 工具完成后的剩余数据读取
While True
ReadCount = fr.ReadBlock(Buffer, 0, BufferSize)
If ReadCount = 0 Then Exit While
fw.Write(Buffer, 0, ReadCount)
End While
fr.Close()
End Using
fs.Close()
End Using
fw.Close()
End Using
End Sub
End Module
namespace 数据源
{
class Program
{
static void Main(string[] args)
{
var filename = "1.txt";
Task.Factory.StartNew(() =>
{
int n = 0;
while (true)
{
for (var i = 0; i < 10000; i++)
{
var s = new string[] { n + "-" + i.ToString() };
if (i == 0) File.WriteAllLines(filename, s);
else File.AppendAllLines(filename, s);
Console.WriteLine(s[0]);
}
n++;
}
});
Console.ReadKey();
}
}
}
namespace 侦听
{
class Program
{
static string filename = "1.txt";
static long last = 0;
static void Main(string[] args)
{
Console.WriteLine(System.IO.Directory.GetCurrentDirectory());
var watcher = new FileSystemWatcher();
watcher.Path = System.IO.Directory.GetCurrentDirectory();
watcher.Filter = filename;
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
Console.ReadKey();
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
string s = "";
FileStream fs = null;
do
{
try
{
fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
}
catch (System.IO.IOException) { }
} while (fs == null);
fs.Position = fs.Length > last ? last : 0;
var sr = new StreamReader(fs);
s = sr.ReadToEnd();
last = fs.Position;
sr.Close();
fs.Close();
sr.Dispose();
fs.Dispose();
Console.WriteLine(s);
}
}
}
不过 数据源 程序会出现自动终止(没有监听时不会)可能是写文件时出现了共享错误,但并没有报错
可见此方案还是存在风险的,因为你无法保证人家就不使用最简单的方式写日志