Where to store custom error log

G

Guest

I want to write some custom error messages to a log so that I can read them
on the next run of the application and as the user to send them to me.

Where the right place to store this log file? Which directory path is the
right one - so that it's always writable regardless of the user's
permissions. I guess it's more of an application-level log than a user-level
log. Or maybe not.

Is there an up-to-date (e.g. .NET 3.5/Vista specific) description of what
the various system directory paths are for?
 
P

Peter Morris

I found this to be a pain in the ass, but here is my code...

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Permissions;

namespace AirSoftware.Common
{
public static class SpecialFolders
{
public static string GetFolderPath(SpecialFolderEx folder)
{
if (!Enum.IsDefined(typeof(SpecialFolderEx), folder))
throw new ArgumentException("Unknown folder: " + folder.ToString());

StringBuilder lpszPath = new StringBuilder(260);
SHGetFolderPath(IntPtr.Zero, (int)folder, IntPtr.Zero, 0, lpszPath);
string path = lpszPath.ToString();
new FileIOPermission(FileIOPermissionAccess.PathDiscovery,
path).Demand();
return path;
}

[DllImport("shfolder.dll", CharSet = CharSet.Auto)]
private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder,
IntPtr hToken, int dwFlags, StringBuilder lpszPath);
}

public enum SpecialFolderEx
{
ApplicationData = 0x1a,
CommonApplicationData = 0x23,
CommonDocuments = 0x2e,
CommonProgramFiles = 0x2b,
Cookies = 0x21,
Desktop = 0,
DesktopDirectory = 0x10,
Favorites = 6,
History = 0x22,
InternetCache = 0x20,
LocalApplicationData = 0x1c,
MyComputer = 0x11,
MyDocuments = 5,
MyMusic = 13,
MyPictures = 0x27,
Personal = 5,
ProgramFiles = 0x26,
Programs = 2,
Recent = 8,
SendTo = 9,
StartMenu = 11,
Startup = 7,
System = 0x25,
Templates = 0x15,
Windows = 0x24
}
}



The code you need is:

string logPath =
SpecialFolders.GetFolderPath(SpecialFolderEx.CommonDocuments);
logPath = Path.Combine(logPath, "MyCompany\MyApp\LogFile.txt");



Pete
 
Z

zacks

I want to write some custom error messages to a log so that I can read them
on the next run of the application and as the user to send them to me.

Where the right place to store this log file? Which directory path is the
right one - so that it's always writable regardless of the user's
permissions. I guess it's more of an application-level log than a user-level
log. Or maybe not.

Is there an up-to-date (e.g. .NET 3.5/Vista specific) description of what
the various system directory paths are for?

string sLogFolder =
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
+ "\\" + Application.CompanyName + "\\" + Application.ProductName

is how I do it.
 
D

DSK Chakravarthy

Create an application event at the event log and store that there. So that
next time you run the app, your app can read that first and from there it
can further go ahead.

HTH
 
A

Alcides

I want to write some custom error messages to a log so that I can read them
on the next run of the application and as the user to send them to me.

Where the right place to store this log file? Which directory path is the
right one - so that it's always writable regardless of the user's
permissions. I guess it's more of an application-level log than a user-level
log. Or maybe not.

Is there an up-to-date (e.g. .NET 3.5/Vista specific) description of what
the various system directory paths are for?

In our application we just write to a network folder. We write a file
per user, whenever we got an exception with some details that usually
the user don't have access or forget to tell, program name, error
message, stack, etc. The user just says the system is not working...:)
Of course this is our case...

Alcides Schulz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top