novotnyllc / MetroLog

A lightweight logging system designed specifically for Windows Store and Windows Phone 8 apps.
MIT License
147 stars 83 forks source link

UnauthorizedAccessException #89

Open a-ney opened 8 years ago

a-ney commented 8 years ago

I'm developing Windows Universal App and UnauthorizedAccessException is thrown every time just after any other exception.

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.IO.WindowsRuntimeStorageExtensions.<OpenStreamForWriteAsyncCore>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.IO.WindowsRuntimeStorageExtensions.<OpenStreamForWriteAsyncCore>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MetroLog.FileTarget.<GetWritableStreamForFile>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MetroLog.Targets.FileTargetBase.<GetOrCreateStreamWriterForFile>d__28.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MetroLog.Targets.FileTargetBase.<WriteAsyncCore>d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MetroLog.Targets.Target.<WriteAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MetroLog.GlobalCrashHandler.<App_UnhandledException>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_0(Object state)
   at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()

I have set up GlobalCrashHandler.Configure(); in App.xaml.cs BTW I use MetroLog with singleton wrapper (if it is important):

public class Logger
    {
        private static Logger _instance;
        private static readonly object SyncRoot = new object();
        private static MetroLog.ILogger _logger;       

        private Logger()
        {
            _logger = LogManagerFactory.DefaultLogManager.GetLogger<Logger>();
        }

        public static Logger Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (SyncRoot)
                    {
                        if (_instance == null)
                            _instance = new Logger();
                    }
                }

                return _instance;
            }
        }        

        public void Warning(string message)
        {
            DebugOutputWrite(message); //duplicating message to Output
            if (_logger.IsWarnEnabled)
                _logger.Info(message);
        }
    }
sfoxover commented 8 years ago

I get this because its writing to the same log file from more than one process. I had to get one process to change the log name to get rid of this issue. Changing the log name is not easy as those class variables are not exposed.

// change a log name var target = new StreamingFileTarget(); if(includeName) target.FileNamingParameters.IncludeLogger = true; LogManagerFactory.DefaultConfiguration.AddTarget(LogLevel.Trace, LogLevel.Fatal, target);

iViktor commented 7 years ago

Hi, I had the same issue, with my application crashing due to Unauthorized: Access denied exceptions. In my case it was because of my own code on app launch, simple mistake, I was not considering other code than mine. I wanted to delete all files older than 2 month on app start, but Metrolog wanted access for logging, and resulted in these exceptions. Metrolog on its own is handling multithreading nicely, I just messed it up with my code. So, asure your code won't interfere with Metrologs routines. Your issue is more than a year old, but maybe this will help other people.