Code Coverage by Clover.NET

Unicode in path for log file

View: New views
2 Messages — Rating Filter:   Alert me  

Unicode in path for log file

by Trebuchet :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi - I have a nlog config file as shown below. However when there are unicode characters in the file then the file never appears in that folder - take out the "odd" characters and it works just fine.  Anyone know what I'm doing wrong?


<?xml version="1.0" encoding="utf-8"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="logFile" xsi:type="File" fileName="C:\UnicodeĆñāćـلـلغلف\Server.log" archiveEvery="Day" archiveFileName="C:\UnicodeĆñāćـلـلغلف\Server.log.{#####}.log" maxArchiveFiles="7" concurrentWrites="true" encoding="utf-8" layout="${longdate}|${level}|${message} " />
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="logFile" />
  </rules>
</nlog>

Re: Unicode in path for log file

by Trebuchet :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Given no response here I went digging myself...

This comes down to Win32FileHelper using CreateFile rather than CreateFileW.  

Changing the import to

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern IntPtr CreateFileW(
            [MarshalAs(UnmanagedType.LPWStr)] string lpwFileName,
            FileAccess dwDesiredAccess,
            int dwShareMode,
            IntPtr lpSecurityAttributes,
            CreationDisposition dwCreationDisposition,
            Win32FileAttributes dwFlagsAndAttributes,
            IntPtr hTemplateFile);

and then changing BaseFileAppender to call this instead seemed to work ok.