Sandeep
Sandeep

Reputation: 7334

Create a Log for WPF Application on client machine?

Is it possible to create a text file and log some info in WPF application when it is deployed as MSI installer and running on a client machine.

I tried creating a file using File.create etc. But the file is not getting created on client machine.

Thanks in advance.

                        using (FileStream stream = File.Create(@"c:\Log.txt"))
                        {
                            using (StreamWriter writer = new StreamWriter(stream))
                            {
                                writer.WriteLine("I can write to local disk.");
                            }
                        }

Upvotes: 0

Views: 9983

Answers (2)

bryanmac
bryanmac

Reputation: 39306

One option is the tracing APIs in .NET. You can configure it via config files to go to a file (file listener). The default goes to debug output and you can use DebugView from Sysinternals. You can even create custom trace listeners.

This link has some overview links:
https://learn.microsoft.com/en-us/archive/blogs/kcwalina/tracing-apis-in-net-framework-2-0

Upvotes: 2

competent_tech
competent_tech

Reputation: 44971

You should be able to use and application-specific directory in the user's local application data store:

        string sDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MyApplicationDir");

        if (!Directory.Exists(sDirectory))
        {
            Directory.CreateDirectory(sDirectory);
        }

        using (FileStream stream = File.Create(Path.Combine(sDirectory, "Log.txt"))
        {
            using (StreamWriter writer = new StreamWriter(stream))
            {
                writer.WriteLine("I can write to local disk.");
            }
        }

Upvotes: 1

Related Questions