My C# DLL configures NLog to save to a file:
private static void SetupLogging()
{
var Config = new NLog.Config.LoggingConfiguration();
var logfile = new NLog.Targets.FileTarget("logfile")
{
FileName = m_LogFile,
WriteBom = true,
Layout = "${longdate}|${level:uppercase=true}|${logger}|${message:withException=true:exceptionSeparator=|}"
};
var logconsole = new NLog.Targets.ConsoleTarget("logconsole");
Config.AddRule(LogLevel.Info, LogLevel.Fatal, logconsole);
Config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile);
LogManager.Configuration = Config;
}
My C++ application uses the DLL and performs various tasks. When it detects that there is an error it attempts to display the error log. It does this:
CFile file{};
CFileException ex{};
if (!file.Open(filePath, CFile::modeRead, &ex))
{
TCHAR szError[_MAX_PATH]{};
ex.GetErrorMessage(&szError[0], _MAX_PATH);
AfxMessageBox(&szError[0], MB_OK | MB_ICONERROR);
return;
}
The problem is that it raises an exception:
Yet, whilst that message is displayed I can actually open the file with NotePad.
So how can I get my application to open the log without getting this sharing violation?
I had to change how I was opening the log file:
if (!file.Open(filePath, CFile::modeRead | CFile::shareDenyNone, &ex))
{
TCHAR szError[_MAX_PATH]{};
ex.GetErrorMessage(&szError[0], _MAX_PATH);
AfxMessageBox(&szError[0], MB_OK | MB_ICONERROR);
return;
}
I needed to use CFile::shareDenyNone
(no sharing restrictions).