void RefreshArchiveLocation()
        {
            var templateService = new TemplateProcessingService();

            var archivePath = templateService.ProcessTemplate(LocationPattern);
            ArchiveLocation = new DirectoryInfo(archivePath);

            if (!ArchiveLocation.Exists)
                ArchiveLocation.Create();
        }
        public override void Initialize()
        {
            if (Archive != null)
                Archive.Attach(this);
            
            var templateService = new TemplateProcessingService();

            var fileDirectoryPath = templateService.ProcessTemplate(FileLocationPattern);
            var fileName = templateService.ProcessTemplate(FileNamePattern);

            LogFile = new FileInfo(Path.Combine(fileDirectoryPath, fileName));

            Directory.CreateDirectory(fileDirectoryPath);

            if(FileLockingStrategy == null)
            {
                Logger.Warning(() => "File Locking Strategy not specified, Single Process Locking will be used for " + SinkLocation.ToString());
                FileLockingStrategy = SingleProcessLockingStrategy.Instance;
            }

            if (BeforeInitialized != null)
                BeforeInitialized(this, EventArgs.Empty);
        }
        public PatternFormatter()
        {
            Serializer.GetOrCreateTypeSerializationStrategy<DiagnosticEventPropertyCollection>()
                .IgnoreMember(x => x.Keys)
                .IgnoreMember(x => x.Values);

            Pattern = "{Event.Message}";

            TextTemplateProcessingServce = new TemplateProcessingService();
            TextTemplateProcessingServce.InternalContext.Functions.AddFunction(
                new Function(
                    "dump",
                    (originalValue, input, parameters) =>
                    {
                        var xml = Serializer.Serialize(input);

                        return xml.ToString();
                    }));
            //TextTemplateProcessingServce.InternalContext.Functions.AddFunction(
            //    new Function(
            //        "dumpHtml",
            //        (originalValue, input, parameters) =>
            //        {
            //            return input.DumpToHtml();
            //        }));

            TextTemplateProcessingServce.InternalContext.Functions.AddFunction(
                new Function(
                    "dumpWithHeader",
                    (originalValue, input, parameters) =>
                    {
                        var xml = Serializer.Serialize(input);

                        return xml.ToString();

                        // TODO:
                        //throw new NotImplementedException();

                        //var kvp = new KeyValuePair(parameters.First().ToString().Trim(new char[] { '\'' }), input);

                        //// todo: apply descriptors
                        //var result = new StringObjectWriter().Write(kvp);

                        //return result;
                    }));

            // Event.Exception.DeepOriginHash - Type + TargetSite of top + all inner
            // Event.Exception.ShallowOriginHash - Type + TargetSite of top
            // Event.Exception.DeepStackTraceHash - Type + StackTrace of top + all inner
            // Event.Exception.ShallowStackTraceHash
            // Event.Exception.DeepHash - Type + Message + Stack Trace of top + all inner

            TextTemplateProcessingServce.InternalContext.Substitutions.AddOrUpdateSubstitution(
                new EvaluatedSubstitution("Event.DeepStackTraceHash",
                    (context) =>
                    {
                        var substitution = (Substitution)null;
                        if (!context.Substitutions.TryGetSubstitution("Event.Exception", out substitution))
                            return "";

                        var ex = substitution.EvaluateOutput(context) as Exception;

                        if (ex == null)
                            return "";

                        var result = ex.DeepStackTraceHash();

                        return result;
                    }));
        }