Click here to Skip to main content
15,908,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, below I have pasted my code. I'm trying to solve the issue but have not been able to do so.
Please help me.
I have marked code lines that produce errors.
C#
public void WriteToErrorLog(Exception ex, string src)
{
    string name = Environment.UserName;
    string domain = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    NTAccount act = new NTAccount(domain);
    DirectoryInfo dirinfo = new DirectoryInfo(logFile);
    DirectorySecurity sec = dirinfo.GetAccessControl();

    // FileSecurity sec = File.GetAccessControl(logFile);
    sec.AddAccessRule(
        new FileSystemAccessRule(
            act,
            FileSystemRights.Write | FileSystemRights.ListDirectory,
            InheritanceFlags.ContainerInherit,
            PropagationFlags.InheritOnly,
            AccessControlType.Allow
        )
    );
    //File.SetAccessControl(logFile, sec); 
    //if (permissionset.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet))
    //{
    StreamWriter streamwr = new StreamWriter(logFile, true); //Error: access denied

    //new FileStream(logFile, FileMode.Append, FileAccess.Write)
    streamwr.WriteLine("------{0}-------", DateTime.Now);
    if (ex.InnerException != null)
    {
        streamwr.Write("Inner exeption Type: ");
        streamwr.WriteLine(ex.InnerException.GetType().ToString());
        streamwr.Write("Inner exeption: ");
        streamwr.WriteLine(ex.InnerException.Message);
        streamwr.Write("Inner Source: ");
        streamwr.WriteLine(ex.InnerException.Source);
        streamwr.WriteLine(ex.InnerException.StackTrace);
    }

    streamwr.Write("Exception Type: ");
    streamwr.WriteLine(ex.GetType().ToString());
    streamwr.WriteLine("Exception: " + ex.Message);
    streamwr.WriteLine("Source: " + src);
    streamwr.WriteLine("Stack Trace: ");
    if (ex.StackTrace != null)
    {
        streamwr.WriteLine(ex.StackTrace);
        streamwr.WriteLine();
    }
    streamwr.Close();
}
Posted
Updated 23-Jul-13 23:25pm
v3
Comments
lukeer 24-Jul-13 5:27am    
Please, don't type in all-capital letters. It's considered shouting in this forum (as elsewhere) and therefore rude.

Wrap code blocks in those tags: <pre lang="c#">YourCodeHere();</pre>
It makes code much more readable and so increases your chance of getting an answer.
FTFY this time.
Pradeepkumar patil sonu 24-Jul-13 5:31am    
Thanks a lot Bro...
Sushil Mate 24-Jul-13 5:31am    
do you have rights to append the data in the log file.
Pradeepkumar patil sonu 24-Jul-13 5:35am    
ya i have set permission with full control for that file..
Sushil Mate 24-Jul-13 5:40am    
how about adding another one AppendData to FileSystemRights.

What I see here is that you have a variable logfile that you're writing to as if it was actually a file.

Nevertheless you try to get a DirectoryInfo from it. That mismatch may cause your headache. Try with a FileInfo instead and tell us if that works.
 
Share this answer
 
Comments
lukeer 24-Jul-13 5:57am    
As I've already said, code belongs in "pre" tags. That doesn't work in comments. But there's a logic to that: post code changes in your original question using the green "Improve question" link directly beneath the question. Then leave a short comment here.

The result is that I automatically get a notification that you commented on my solution. And your question is updated for all potential helpers to see. Not all of them would read the changed code in a comment to some other folk's solution.
Pradeepkumar patil sonu 24-Jul-13 6:14am    
i tried by using Fileinfo but same error m getting
i just pasted my errorlog.text page from Error folder to my project solution.. now its working fine
but still i didn't get why its not working if my error.text file inside error folder..
 
Share this answer
 

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