Ejemplo n.º 1
0
        internal void InternalInit()
        {
            ContextTraceListener contextListener = ((ContextTraceListener)Trace.Listeners["ContextTraceListener"]);

            if (contextListener != null)
            {
                if (!contextListener.IsContextInitialized(this.name))
                {
                    try
                    {
                        contextListener.InitializeContext(this.name,
                                                          new RollingFileTraceListener(logType.FileName, logType.MaxFileSize));
                    }
                    catch (IOException ex)
                    {
                        contextListener.InitializeContext(this.name,
                                                          new TextWriterTraceListener(Console.Out));

                        if (Tracing.TraceError)
                        {
                            Trace.WriteLine("Failed to initialize file logging, directing to console instead.");
                            Trace.WriteLine(ex);
                        }
                    }
                }
            }

            Init();
        }
        /// <summary>
        /// Called by the pipeline to invoke the component.
        /// </summary>
        /// <param name="msg">The message to process.</param>
        /// <returns>A collection of messages produced by this component.</returns>
        public override Collection <MultiPartMessage> Invoke(MultiPartMessage msg)
        {
            Collection <MultiPartMessage> resultCollection = new Collection <MultiPartMessage>();

            resultCollection.Add(msg);

            if (MessageEngine.Instance.Tracing.Switch.Level != SourceLevels.Off)
            {
                ContextTraceListener contextListener = ((ContextTraceListener)MessageEngine.Instance.Tracing.Listeners["ContextTraceListener"]);

                if (contextListener != null)
                {
                    string sessionId = msg.Properties.ReadAsString("SerialNumber");

                    if ((MessageEngine.Instance.Tracing.Switch.Level & SourceLevels.Verbose) == SourceLevels.Verbose)
                    {
                        MessageEngine.Instance.Tracing.TraceData(TraceEventType.Verbose, 0, string.Format("TraceContextComponent: Switching context to: \"{0}\"", sessionId));
                    }

                    if (!contextListener.IsContextInitialized(sessionId))
                    {
                        contextListener.InitializeContext(sessionId,
                                                          new RollingFileTraceListener(Path.Combine(logPath, sessionId + ".log"), maxLogSize));
                    }

                    contextListener.Context = sessionId;
                }
            }

            return(resultCollection);
        }
        /// <summary>
        /// Called by the pipeline to invoke the component.
        /// </summary>
        /// <param name="msg">The message to process.</param>
        /// <returns>A collection of messages produced by this component.</returns>
        public override Collection <MultiPartMessage> Invoke(MultiPartMessage msg)
        {
            Collection <MultiPartMessage> resultCollection = new Collection <MultiPartMessage>();

            resultCollection.Add(msg);

            if (MessageEngine.Instance.Tracing.Switch.Level != SourceLevels.Off)
            {
                ContextTraceListener contextListener = ((ContextTraceListener)MessageEngine.Instance.Tracing.Listeners["ContextTraceListener"]);

                if (contextListener != null)
                {
                    contextListener.Context = null;

                    string sessionId = msg.Metadata.ReadAsString("SessionId");

                    ClientSession session = SessionManager.Instance[sessionId];

                    if (session != null)
                    {
                        string name = null;
                        string ip   = null;

                        lock (session.SyncLock)
                        {
                            name = session.TerminalId;
                            ip   = session.ClientIP;
                        }

                        if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(ip))
                        {
                            string context = string.Format("{0}_{1}", name, ip.Replace('.', '_'));

                            if ((MessageEngine.Instance.Tracing.Switch.Level & SourceLevels.Verbose) == SourceLevels.Verbose)
                            {
                                MessageEngine.Instance.Tracing.TraceData(TraceEventType.Verbose, 0, string.Format("TraceContextComponent: Switching context to: \"{0}\"", context));
                            }

                            if (!contextListener.IsContextInitialized(context))
                            {
                                contextListener.InitializeContext(context,
                                                                  new RollingFileTraceListener(Path.Combine(logPath, context + ".log"), maxLogSize));
                            }

                            contextListener.Context = context;
                        }
                    }
                }
            }

            return(resultCollection);
        }