Exemplo n.º 1
0
 internal void _Log(C8oLogLevel logLevel, string message, Exception exception = null)
 {
     if (c8o.LogC8o)
     {
         Log(logLevel, LOG_INTERNAL_PREFIX + message, exception);
     }
 }
Exemplo n.º 2
0
        internal void Log(C8oLogLevel logLevel, string message, Exception exception = null)
        {
            bool isLogConsole = IsLoggableConsole(logLevel);
            bool isLogRemote  = IsLoggableRemote(logLevel);

            if (isLogConsole || isLogRemote)
            {
                if (exception != null)
                {
                    message += "\n" + exception;
                }

                string time = (DateTime.Now.Subtract(startTimeRemoteLog).TotalSeconds + "").Replace(",", ".");
                time = RE_FORMAT_TIME.Replace(time, "$1.$2");

                if (isLogRemote)
                {
                    remoteLogs.Enqueue(new JObject()
                    {
                        { JSON_KEY_TIME, time },
                        { JSON_KEY_LEVEL, logLevel.name },
                        { JSON_KEY_MESSAGE, message }
                    });
                    LogRemote();
                }

                if (isLogConsole)
                {
                    System.Diagnostics.Debug.WriteLine("(" + time + ") [" + logLevel.name + "] " + message);
                }
            }
        }
Exemplo n.º 3
0
        internal C8oLogger(C8o c8o)
        {
            this.c8o = c8o;

            remoteLogUrl         = c8o.EndpointConvertigo + "/admin/services/logs.Add";
            remoteLogs           = new Queue <JObject>();
            alreadyRemoteLogging = new bool[] { false };

            remoteLogLevel = C8oLogLevel.TRACE;

            var currentTime = DateTime.Now;

            startTimeRemoteLog = currentTime;
            uidRemoteLogs      = C8oTranslator.DoubleToHexString(C8oUtils.GetUnixEpochTime(currentTime));
            env = new JObject()
            {
                { "uid", uidRemoteLogs },
                { "uuid", c8o.DeviceUUID },
                { "project", c8o.EndpointProject }
            }.ToString();
        }
Exemplo n.º 4
0
 /// <summary>
 /// Sets a value indicating the log level you want in the device console
 /// You should use C8oLogLevel constants
 /// </summary>
 /// <param name="logLevelLocal"></param>
 /// <returns>The current<c>C8oSettings</c>, for chaining.</returns>
 public C8oSettings SetLogLevelLocal(C8oLogLevel logLevelLocal)
 {
     this.logLevelLocal = logLevelLocal;
     return(this);
 }
Exemplo n.º 5
0
        protected void Copy(C8oBase c8oBase)
        {
            //*** HTTP ***//

            timeout             = c8oBase.timeout;
            trustAllCetificates = c8oBase.trustAllCetificates;

            if (c8oBase.cookies != null)
            {
                if (cookies == null)
                {
                    cookies = new CookieCollection();
                }
                cookies.Add(c8oBase.cookies);
            }

            if (c8oBase.clientCertificateBinaries != null)
            {
                if (clientCertificateBinaries == null)
                {
                    clientCertificateBinaries = new Dictionary <byte[], string>(c8oBase.clientCertificateBinaries);
                }
                else
                {
                    foreach (var entry in c8oBase.clientCertificateBinaries)
                    {
                        clientCertificateBinaries.Add(entry.Key, entry.Value);
                    }
                }
            }

            if (c8oBase.clientCertificateFiles != null)
            {
                if (clientCertificateFiles == null)
                {
                    clientCertificateFiles = new Dictionary <string, string>(c8oBase.clientCertificateFiles);
                }
                else
                {
                    foreach (var entry in c8oBase.clientCertificateFiles)
                    {
                        clientCertificateFiles.Add(entry.Key, entry.Value);
                    }
                }
            }

            //*** Log ***//

            logRemote     = c8oBase.logRemote;
            logLevelLocal = c8oBase.logLevelLocal;
            logC8o        = c8oBase.logC8o;
            logOnFail     = c8oBase.logOnFail;

            //*** FullSync ***//

            defaultDatabaseName       = c8oBase.defaultDatabaseName;
            authenticationCookieValue = c8oBase.authenticationCookieValue;
            fullSyncLocalSuffix       = c8oBase.fullSyncLocalSuffix;
            fullSyncStorageEngine     = c8oBase.fullSyncStorageEngine;
            fullSyncEncryptionKey     = c8oBase.fullSyncEncryptionKey;

            fullSyncServerUrl = c8oBase.fullSyncServerUrl;
            fullSyncUsername  = c8oBase.fullSyncUsername;
            fullSyncPassword  = c8oBase.fullSyncPassword;

            fullSyncReplicationHeartbeat              = c8oBase.fullSyncReplicationHeartbeat;
            fullSyncReplicationSocketTimeout          = c8oBase.fullSyncReplicationSocketTimeout;
            fullSyncReplicationRequestTimeout         = c8oBase.fullSyncReplicationRequestTimeout;
            fullSyncReplicationMaxOpenHttpConnections = c8oBase.fullSyncReplicationMaxOpenHttpConnections;
            fullSyncReplicationMaxRevsToGetInBulk     = c8oBase.fullSyncReplicationMaxRevsToGetInBulk;
            fullSyncReplicationRetryDelay             = c8oBase.fullSyncReplicationRetryDelay;

            uiDispatcher = c8oBase.uiDispatcher;
        }
Exemplo n.º 6
0
 private bool IsLoggableConsole(C8oLogLevel logLevel)
 {
     return(logLevel != null && C8oLogLevel.TRACE.priority <= c8o.LogLevelLocal.priority && c8o.LogLevelLocal.priority <= logLevel.priority);
 }
Exemplo n.º 7
0
 private bool IsLoggableRemote(C8oLogLevel logLevel)
 {
     return(c8o.LogRemote && logLevel != null && C8oLogLevel.TRACE.priority <= remoteLogLevel.priority && remoteLogLevel.priority <= logLevel.priority);
 }
Exemplo n.º 8
0
        private void LogRemote()
        {
            bool canLog = false;

            lock (alreadyRemoteLogging)
            {
                // If there is no another thread already logging AND there is at least one log
                canLog = !alreadyRemoteLogging[0] && remoteLogs.Count > 0;
                if (canLog)
                {
                    alreadyRemoteLogging[0] = true;
                }
            }

            if (canLog)
            {
                c8o.RunBG(async() =>
                {
                    try
                    {
                        // Take logs in the queue and add it to a json array
                        int count     = 0;
                        int listSize  = remoteLogs.Count;
                        var logsArray = new JArray();

                        while (count < listSize && count < REMOTE_LOG_LIMIT)
                        {
                            logsArray.Add(remoteLogs.Dequeue());
                            count++;
                        }

                        // Initializes request paramters
                        var parameters = new Dictionary <string, object>()
                        {
                            { JSON_KEY_LOGS, JsonConvert.SerializeObject(logsArray) },
                            { JSON_KEY_ENV, env },
                            { C8o.ENGINE_PARAMETER_DEVICE_UUID, c8o.DeviceUUID }
                        };

                        JObject jsonResponse;
                        try
                        {
                            var webResponse    = await c8o.httpInterface.HandleRequest(remoteLogUrl, parameters);
                            var streamResponse = webResponse.GetResponseStream();
                            jsonResponse       = C8oTranslator.StreamToJson(streamResponse);
                        }
                        catch (Exception e)
                        {
                            c8o.LogRemote = false;
                            if (c8o.LogOnFail != null)
                            {
                                c8o.LogOnFail(new C8oException(C8oExceptionMessage.RemoteLogFail(), e), null);
                            }
                            return;
                        }

                        if (jsonResponse != null)
                        {
                            var logLevelResponse = jsonResponse.GetValue(C8oLogger.JSON_KEY_REMOTE_LOG_LEVEL);
                            if (logLevelResponse != null)
                            {
                                string logLevelResponseStr = logLevelResponse.Value <string>();
                                var c8oLogLevel            = C8oLogLevel.GetC8oLogLevel(logLevelResponseStr);

                                if (c8oLogLevel != null)
                                {
                                    remoteLogLevel = c8oLogLevel;
                                }
                                LogRemote();
                            }
                        }
                    }
                    finally
                    {
                        lock (alreadyRemoteLogging)
                        {
                            alreadyRemoteLogging[0] = false;
                        }
                    }
                });
            }
        }
Exemplo n.º 9
0
 //*** Basics log ***//
 public bool CanLog(C8oLogLevel logLevel)
 {
     return(IsLoggableConsole(logLevel) || IsLoggableRemote(logLevel));
 }