如何创建任务以同时写入文件以进行日志记录 [英] how to create tasks to write file in same time for logging
问题描述
我想要 在许多情况下,在文件中写日志消息应该创建许多要同时写入的任务,方法是使用FileIO.AppendTextAsync将文本附加到文件中。
但是当我这样做时,  ;错误已经发生 , 例外:HRESULT:0x80070005,说该文件是由othen线程打开的,无法访问。
void CLOG :: Open(String ^ fileName)
{
ifExit = false;
auto CreateFolderFN = KnownFolders :: DocumentsLibrary-> CreateFolderAsync(" MCloudWin8",CreationCollisionOption :: OpenIfExists);
任务< StorageFolder ^>(CreateFolderFN)。然后
([fileName,this](StorageFolder ^ MCloudLogFolder)
{
testStr ="" ;;
FinishCreateFileDo + = ref new FinishCreateFileHandler(this,& CLOG :: FinishCreateFileRespone);
auto calendar = ref new Windows :: Globalization :: Calendar();
String ^ buf = fileName + calendar-> YearAsString ()+ calendar-> MonthAsNumericString()
+ calendar-> DayAsString()+" .log" ;;
return MCloudLogFolder-> CreateFileAsync(buf,CreationCollisionOption :: OpenIfExists);
})。then([this](StorageFile ^ file)
{
m_LogFile = file;
ifExit = true;
FinishCreateFileDo();
}) .then([](task< void> t)
{
try
{
t.get();
}
catch(平台: :例外^ e)
{
OutputDebugString(e-> Message-> ToString() - > Data());
}
}, task_continuation_context :: use_arbitrary());
}
void CLOG :: LogOut(Platform :: String ^ strToWrite)
{
task< String ^>([strToWrite](){
auto calendar = ref new Windows :: Globalization :: Calendar();
String ^ buf = calendar-> YearAsString()+" - " + calendar-> MonthAsNumericString()+" - " + calendar-> DayAsString()
+"" + calendar-> HourAsString()+":" + calendar - > MinuteAsString()
+":" + calendar-> SecondAsString()+":" + calendar-> NanosecondAsString()+
strToWrite +" \ r \ n" ;;
返回buf;
})。then([this,strToWrite](String ^ buf)
{
if(ifExit == true) )
{
//返回FileIO :: AppendTextAsync(m_LogFile,buf);
create_task(FileIO :: AppendTextAsync(m_LogFile,buf))。then([buf,this](task< void> t)
{
try
{
t.get();
}
catch(Platform :: Exception ^ e)
{
OutputDebugStr ING(E-> MESSAGE->的ToString() - >数据());
}
});
}
其他
{
strBuf = strBuf + buf;
}
})。then([this,strToWrite](task< void> t)
{
try
{
t.get() ;
}
catch(Platform :: Exception ^ e)
{
OutputDebugString(e-> Message-> ToString() - > Data());
// strBuf + = strToWrite;
}
},task_continuation_context :: use_arbitrary());
// Windows :: Storage :: FileIO :: WriteTextAsync(m_LogFile,strToWrite)
}
< pre class ="prettyprint"> void CLOG :: FinishCreateFileRespone()
{
task< void>
(
FileIO :: AppendTextAsync(m_LogFile,strBuf)
).then([this]()
{
strBuf ="" ;;
})。then([](task< void> t)
{
try
{
t.get();
}
catch (例外^ e)
{
OutputDebugString(e-> Message-> ToString() - > Data());
}
},task_continuation_context :: use_arbitrary ());
}
MainPage :: MainPage()
{
InitializeComponent() ;
CLOG :: instance-> Open(" MCloudLog");
int a = 170;
String ^ p =" a" ;;
而(a)
{
a--;
p + =" b" ;;
// test(p);
CLOG :: instance-> LogOut(" testTTT" + p);
wait(0.1); //如果我等待(1)没问题,但小于1异常出现。登录我的程序的间隔时间始终小于1ms,随时登录
CLOG ::实例 - >退出(" testTTT" + p);
等(0.1);
CLOG :: instance-> LogOut(" testTTT" + p);
等(0.1);
}
}
我真的无法找到关闭文件的任何方法,检查文件是否正在使用,以及如何锁定?疯了, 任何人都可以帮助我??? thanksssssssssss!
i want to write log message in file, in many case,should create many tasks to write in same time, by appending text into the file using FileIO.AppendTextAsync.
but when i do, error has occurred , Exception:HRESULT:0x80070005,saying the file is opened by othen thread,can't access.
void CLOG::Open(String^ fileName) { ifExit=false; auto CreateFolderFN = KnownFolders::DocumentsLibrary->CreateFolderAsync("MCloudWin8",CreationCollisionOption::OpenIfExists); task<StorageFolder^>(CreateFolderFN).then ([fileName,this](StorageFolder^ MCloudLogFolder) { testStr=""; FinishCreateFileDo+=ref new FinishCreateFileHandler(this,&CLOG::FinishCreateFileRespone); auto calendar = ref new Windows::Globalization::Calendar(); String^ buf= fileName+calendar->YearAsString()+calendar->MonthAsNumericString() +calendar->DayAsString()+".log"; return MCloudLogFolder->CreateFileAsync(buf, CreationCollisionOption::OpenIfExists); }).then([this](StorageFile^file) { m_LogFile=file; ifExit=true; FinishCreateFileDo(); }).then([](task<void> t) { try { t.get(); } catch(Platform::Exception^ e) { OutputDebugString(e->Message->ToString()->Data()); } },task_continuation_context::use_arbitrary()); }
void CLOG::LogOut(Platform::String^ strToWrite) { task<String^>([strToWrite](){ auto calendar = ref new Windows::Globalization::Calendar(); String^buf= calendar->YearAsString()+"-"+calendar->MonthAsNumericString()+"-"+calendar->DayAsString() +" "+calendar->HourAsString()+":"+calendar->MinuteAsString() +":"+calendar->SecondAsString()+":"+calendar->NanosecondAsString()+ strToWrite+"\r\n"; return buf; }).then([this,strToWrite](String^ buf) { if(ifExit==true) { //return FileIO::AppendTextAsync(m_LogFile, buf); create_task(FileIO::AppendTextAsync(m_LogFile, buf)).then([buf,this](task<void> t) { try { t.get(); } catch(Platform::Exception^ e) { OutputDebugString(e->Message->ToString()->Data()); } }); } else { strBuf=strBuf+buf; } }).then([this,strToWrite](task<void> t) { try { t.get(); } catch(Platform::Exception^ e) { OutputDebugString(e->Message->ToString()->Data()); //strBuf+=strToWrite; } },task_continuation_context::use_arbitrary()); //Windows::Storage::FileIO::WriteTextAsync(m_LogFile, strToWrite) }
void CLOG::FinishCreateFileRespone() { task<void> ( FileIO::AppendTextAsync(m_LogFile, strBuf) ).then([this]() { strBuf=""; }).then([](task<void> t) { try { t.get(); } catch (Exception^e) { OutputDebugString(e->Message->ToString()->Data()); } },task_continuation_context::use_arbitrary()); }
MainPage::MainPage() { InitializeComponent(); CLOG::instance->Open("MCloudLog"); int a=170; String ^p="a"; while (a) { a--; p+="b"; //test(p); CLOG::instance->LogOut("testTTT"+p); wait(0.1);//if I wait(1) is OK,but less than 1 Exception arise.and interval time to log in my Program always less than 1ms, to loging in any time CLOG::instance->LogOut("testTTT"+p); wait(0.1); CLOG::instance->LogOut("testTTT"+p); wait(0.1); } }
i really can not find any methods to close the file ,check if the file is be using ,and how can i make a lock? being crazy, can anyone help me ???thanksssssssssss!
这篇关于如何创建任务以同时写入文件以进行日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!