Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am logging in my WPF application using TextWriterTraceListener. The file is stored in csv format. I am writing text in hebrew language. The problem is that when I open that csv file in MS Excel it open as shown in picture below which is not correct. But when i save the file again as UTF8 format using a Notepad. It opens perfectly fine. I think tracelistner is not saving csv file in UTF8 format. How should i fix this?

Excelsheet snapshot link: http://ge.tt/7CLTAGh1/v/0?c

App.config code
XML
<system.diagnostics>
    <trace autoflush="true" indentsize="3">
      <listeners>
        <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\POSH\AnnaAssetData\ANNALog2.csv" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>



C# code:

C#
var line   = string.Format("{0},{1},{2},{3}",DateTime.Now, Thread.CurrentThread.CurrentUICulture, view,extraData);
Trace.WriteLine(line);


Logger App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="Audit">
    <listeners>
      <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          source="Enterprise Library Logging" formatter="Text Formatter"
          log="" machineName="." traceOutputOptions="None" />
      <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          fileName="Audit.log" rollInterval="Day" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
          template="Timestamp: {timestamp}{newline}Message: {message}{newline}Category: {category}{newline}Priority: {priority}{newline}Severity: {severity}{newline}Title:{title}{newline}Extended Properties: {dictionary({key} - {value}{newline})}"
          name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="Audit">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </add>
      <add switchValue="Error" name="Exception">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors & Warnings">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
</configuration>
Posted
Comments
Prasad Khandekar 16-May-14 7:20am    
Hi,

The TextWriterTraceListener class uses UTF-8 encoding by default. It's the excel which is not able to recognize the encoding as the resulting file does not contain UTF-8 preamble. While importing the file however you can select the File Origin as 65001: Unicode (UTF-8). File data should then get displayed correctly.

Regards,
posh_BalrajS 16-May-14 7:37am    
How can I achieve this in my code.
Actually my aim is when I log my data in csv and anyone opens it they should be able to see data in correct format without any changes. How can i correct my current implementation.
Prasad Khandekar 18-May-14 13:35pm    
Hello,

You will have to create your own TraceListener. This custom tracelistener can delegate the work to TextWriterTraceListener and this instance can be created using following syntax.

TextWriterTraceListener listener = new TextWriterTraceListener(new StreamWriter(@"C:\AppTrace.log", false, new UTF8Encoding(true)));

In your config file configure the custom tracelistener instead.

Regards,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900