As the stack traces says you are not able to access that folder, so the basic solution is provide access to the particular folder .
follow these steps ;
1. Right Click on log folder --> properties --> security --> edit -- > add --> now add everyone in the list.
2. if it is a hosted application add IUSER_<machinename> in security tab mentioned above.
3. If above 2 steps doesnt work try to impersonate the current user by some other user.
you can achieve this by refering the below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;
using System.Security.Principal;
namespace WebApplication1
{
public class ImpersonationService
{
#region Consts
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
#endregion
#region External API
[DllImport("advapi32.dll", SetLastError = true)]
public static extern int LogonUser(
string lpszUsername, string lpszDomain,
string lpszPassword, int dwLogonType,
int dwLogonProvider, out IntPtr phToken
);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool RevertToSelf();
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int CloseHandle(IntPtr hObject);
#endregion
#region Methods
public void PerformImpersonatedTask(string username, string domain,
string password, int logonType, int logonProvider, Action methodToPerform)
{
IntPtr token = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUser(username, domain, password, logonType,
logonProvider, out token) != 0)
{
var identity = new WindowsIdentity(token);
var impersonationContext = identity.Impersonate();
if (impersonationContext != null)
{
methodToPerform.Invoke();
impersonationContext.Undo();
}
}
else
{
}
}
if (token != IntPtr.Zero)
{
CloseHandle(token);
}
}
#endregion
}
}
Use this class as
using System;
using System.IO;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
#region Page Events
private void Page_Load(object sender, System.EventArgs e)
{
var service = new ImpersonationService();
service.PerformImpersonatedTask("username", "domain", "password",
ImpersonationService.LOGON32_LOGON_INTERACTIVE,
ImpersonationService.LOGON32_PROVIDER_DEFAULT,
new Action(MethodToPerform));
}
#endregion
#region Methods
public void MethodToPerform()
{
var serverPath = @"\\ServerName\test";
var dirInfo = new DirectoryInfo(serverPath);
Response.Write(dirInfo.Exists);
}
#endregion
}
}