public static void AddNLogLogging(this IContainer container)
 {
     container.Register <ILoggerFactory, NLogLoggerFactory>(Reuse.Singleton, FactoryMethod.ConstructorWithResolvableArguments);
     if (!container.IsRegistered(typeof(ILogger)))
     {
         container.AddLoggerResolving();
     }
 }
示例#2
0
        private void Start()
        {
            container = new Container();
            container.Register <ILoggerFactory, NLogLoggerFactory>(Reuse.Singleton, FactoryMethod.ConstructorWithResolvableArguments);
            container.AddLoggerResolving();

            container.Register <INHibernateInstaller, NHibInstaller>(Reuse.Singleton);
            container.Register <Logger>(Reuse.Singleton);

            container.AddAutoTx();
            container.AddNHibernate();

            using (var scope = new ResolveScope <Logger>(container))
            {
                using (var up = new ResolveScope <Configuration>(container))
                    new SchemaUpdate(up.Service).Execute(false, true);

                Console.WriteLine("Current log contents:");
                Console.WriteLine("[utc date] - [text]");
                Console.WriteLine("-------------------");
                scope.Service.ReadLog(Console.WriteLine);
                scope.Service.WriteToLog(string.Format("{0} - Started", DateTime.UtcNow));
            }
        }
示例#3
0
        public void Init(IContainer container, AmbientTransactionOption ambientTransaction)
        {
            ILogger _Logger = NullLogger.Instance;

            // check we have a logger factory
            if (container.IsRegistered(typeof(ILoggerFactory)))
            {
                // get logger factory
                var loggerFactory = container.Resolve <ILoggerFactory>();
                // get logger
                _Logger = loggerFactory.CreateLogger(typeof(AutoTxFacility));
            }
            else
            {
                Trace.TraceWarning("Missing ILogger in container; add it or you'll have no logging of errors!");
                container.UseInstance(typeof(ILoggerFactory), NullLoggerFactory.Instance);
            }

            if (_Logger.IsEnabled(LogLevel.Debug))
            {
                _Logger.LogDebug("initializing AutoTxFacility");
            }

            if (!container.IsRegistered(typeof(ILogger)))
            {
                container.AddLoggerResolving();

                if (_Logger.IsEnabled(LogLevel.Debug))
                {
                    _Logger.LogDebug("Added capability of resolving ILogger");
                }
            }

            // add capability to inject info about requested service to the constructor
            container.Register <ProxyTypeStorage>(Reuse.Singleton);
            container.Register(Made.Of(
                                   () => new ParentServiceRequestInfo(Arg.Index <Request>(0), Arg.Of <ProxyTypeStorage>()),
                                   request => request), setup: Setup.With(asResolutionCall: true));

            // register PerTransactionScopeContext to container as singleton (one storage for scope-per-transaction)
            container.Register <PerTransactionScopeContext>(Reuse.Singleton);
            container.Register <PerTopTransactionScopeContext>(Reuse.Singleton);

            // the interceptor needs to be created for every method call
            container.Register <TransactionInterceptor>(Reuse.Transient);
            container.Register <ITransactionMetaInfoStore, TransactionClassMetaInfoStore>(Reuse.Singleton);
            container.RegisterMany(new[] { typeof(ITransactionManager), typeof(TransactionManager) }, typeof(TransactionManager), Reuse.Singleton);

            // the activity manager shouldn't have the same lifestyle as TransactionInterceptor, as it
            // calls a static .Net/Mono framework method, and it's the responsibility of
            // that framework method to keep track of the call context.
            container.Register <IActivityManager, AsyncLocalActivityManager>(Reuse.Singleton);

            // configuration
            container.UseInstance(new AutoTxOptions
            {
                AmbientTransaction = ambientTransaction,
            });

            var componentInspector = new TransactionalComponentInspector(container);

            _Logger.LogDebug(
                "inspecting previously registered components; this might throw if you have configured your components in the wrong way");

            foreach (var serviceRegistrationInfo in container.GetServiceRegistrations())
            {
                componentInspector.ProcessModel(serviceRegistrationInfo);
            }

            container.Register <AutoTxFacility>(Reuse.Transient);            // to determine if AutoTx was initialized

            _Logger.LogDebug(
                @"Initialized AutoTxFacility:

If you are experiencing problems, go to https://github.com/castleproject/Castle.Transactions and file a ticket for the Transactions project.
You can enable verbose logging for .Net by adding this to you .config file:

	<system.diagnostics>
		<sources>
			<source name=""System.Transactions"" switchValue=""Information"">
				<listeners>
					<add name=""tx"" type=""Castle.Transactions.Logging.TraceListener, Castle.Transactions""/>
				</listeners>
			</source>
		</sources>
	</system.diagnostics>

If you wish to e.g. roll back a transaction from within a transactional method you can resolve/use the ITransactionManager's
CurrentTransaction property and invoke Rollback on it. Be ready to catch TransactionAbortedException from the caller. You can enable
debugging through log4net.
");
        }