From 7db480b23e8d11a384b88a34704468341429c562 Mon Sep 17 00:00:00 2001 From: Pawel Kadluczka Date: Tue, 29 Aug 2017 16:28:44 -0700 Subject: [PATCH] Changing the way users can enable logging --- .../Connection.spec.ts | 24 ++++++++++++------- .../LoggerFactory.spec.ts | 24 +++++++++++++++++++ .../HttpConnection.ts | 5 ++-- .../HubConnection.ts | 15 +++++++----- .../IHttpConnectionOptions.ts | 4 ++-- .../IHubConnectionOptions.ts | 10 ++++++++ .../Loggers.ts | 18 ++++++++++++++ .../wwwroot/js/connectionTests.js | 2 +- .../wwwroot/js/hubConnectionTests.js | 19 +++++---------- 9 files changed, 89 insertions(+), 32 deletions(-) create mode 100644 client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/LoggerFactory.spec.ts create mode 100644 client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHubConnectionOptions.ts diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/Connection.spec.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/Connection.spec.ts index 32defd999c..c17a15e5aa 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/Connection.spec.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/Connection.spec.ts @@ -16,7 +16,8 @@ describe("Connection", () => { get(url: string): Promise { return Promise.resolve(""); } - } + }, + logging: null } as IHttpConnectionOptions; let connection = new HttpConnection("http://tempuri.org", options); @@ -51,7 +52,8 @@ describe("Connection", () => { get(url: string): Promise { return Promise.resolve(""); } - } + }, + logging: null } as IHttpConnectionOptions; let connection = new HttpConnection("http://tempuri.org", options); @@ -74,7 +76,8 @@ describe("Connection", () => { get(url: string): Promise { return Promise.resolve(""); } - } + }, + logging: null } as IHttpConnectionOptions; let connection = new HttpConnection("http://tempuri.org", options); @@ -109,7 +112,8 @@ describe("Connection", () => { connection.stop(); return Promise.resolve(""); } - } + }, + logging: null } as IHttpConnectionOptions; let connection = new HttpConnection("http://tempuri.org", options); @@ -154,7 +158,8 @@ describe("Connection", () => { return Promise.resolve(""); } }, - transport: fakeTransport + transport: fakeTransport, + logging: null } as IHttpConnectionOptions; @@ -183,7 +188,8 @@ describe("Connection", () => { return Promise.resolve(""); } }, - transport: requestedTransport + transport: requestedTransport, + logging: null } as IHttpConnectionOptions; let connection = new HttpConnection("http://tempuri.org", options); @@ -208,7 +214,8 @@ describe("Connection", () => { get(url: string): Promise { return Promise.resolve(""); } - } + }, + logging: null } as IHttpConnectionOptions; let connection = new HttpConnection("http://tempuri.org", options); @@ -249,7 +256,8 @@ describe("Connection", () => { return Promise.resolve(""); } }, - transport: fakeTransport + transport: fakeTransport, + logging: null } as IHttpConnectionOptions; let connection = new HttpConnection("https://tempuri.org", options); diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/LoggerFactory.spec.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/LoggerFactory.spec.ts new file mode 100644 index 0000000000..dce2f7fd0a --- /dev/null +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/LoggerFactory.spec.ts @@ -0,0 +1,24 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +import { LoggerFactory } from "../Microsoft.AspNetCore.SignalR.Client.TS/Loggers" +import { ILogger, LogLevel } from "../Microsoft.AspNetCore.SignalR.Client.TS/ILogger" + +describe("LoggerFactory", () => { + it("creates ConsoleLogger when no logging specified", () => { + expect(LoggerFactory.createLogger().constructor.name).toBe("ConsoleLogger"); + }); + + it("creates NullLogger when logging is set to null", () => { + expect(LoggerFactory.createLogger(null).constructor.name).toBe("NullLogger"); + }); + + it("creates ConsoleLogger when log level specified", () => { + expect(LoggerFactory.createLogger(LogLevel.Information).constructor.name).toBe("ConsoleLogger"); + }); + + it("does not create its own logger if the user provides one", () => { + let customLogger : ILogger = { log: LogLevel => {} }; + expect(LoggerFactory.createLogger(customLogger)).toBe(customLogger); + }); +}); \ No newline at end of file diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HttpConnection.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HttpConnection.ts index 7eb6472dc7..641f3845f2 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HttpConnection.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HttpConnection.ts @@ -4,7 +4,7 @@ import { ITransport, TransferMode, TransportType, WebSocketTransport, ServerSent import { IHttpClient, HttpClient } from "./HttpClient" import { IHttpConnectionOptions } from "./IHttpConnectionOptions" import { ILogger, LogLevel } from "./ILogger" -import { NullLogger } from "./Loggers" +import { LoggerFactory } from "./Loggers" const enum ConnectionState { Initial, @@ -32,8 +32,9 @@ export class HttpConnection implements IConnection { constructor(url: string, options: IHttpConnectionOptions = {}) { this.url = url; + options = options || {}; this.httpClient = options.httpClient || new HttpClient(); - this.logger = options.logger || new NullLogger(); + this.logger = LoggerFactory.createLogger(options.logging); this.connectionState = ConnectionState.Initial; this.options = options; } diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts index 87cfb0fae2..2d681793bd 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HubConnection.ts @@ -7,13 +7,14 @@ import { JsonHubProtocol } from "./JsonHubProtocol"; import { TextMessageFormat } from "./Formatters" import { Base64EncodedHubProtocol } from "./Base64EncodedHubProtocol" import { ILogger, LogLevel } from "./ILogger" -import { NullLogger } from "./Loggers" +import { ConsoleLogger, NullLogger, LoggerFactory } from "./Loggers" +import { IHubConnectionOptions } from "./IHubConnectionOptions" export { TransportType } from "./Transports" export { HttpConnection } from "./HttpConnection" export { JsonHubProtocol } from "./JsonHubProtocol" -export { LogLevel } from "./ILogger" -export { ConsoleLogger } from "./Loggers" +export { LogLevel, ILogger } from "./ILogger" +export { ConsoleLogger, NullLogger } from "./Loggers" export class HubConnection { private readonly connection: IConnection; @@ -24,10 +25,12 @@ export class HubConnection { private id: number; private connectionClosedCallback: ConnectionClosed; - constructor(connection: IConnection, logger: ILogger = new NullLogger(), protocol: IHubProtocol = new JsonHubProtocol()) { + constructor(connection: IConnection, options: IHubConnectionOptions = {}) { this.connection = connection; - this.logger = logger || new NullLogger(); - this.protocol = protocol || new JsonHubProtocol(); + options = options || {}; + this.logger = LoggerFactory.createLogger(options.logging); + + this.protocol = options.protocol || new JsonHubProtocol(); this.connection.onDataReceived = data => { this.onDataReceived(data); }; diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHttpConnectionOptions.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHttpConnectionOptions.ts index 0a25deb282..c722ca3a6d 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHttpConnectionOptions.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHttpConnectionOptions.ts @@ -1,9 +1,9 @@ import { IHttpClient } from "./HttpClient" import { TransportType, ITransport } from "./Transports" -import { ILogger } from "./ILogger"; +import { ILogger, LogLevel } from "./ILogger"; export interface IHttpConnectionOptions { httpClient?: IHttpClient; transport?: TransportType | ITransport; - logger?: ILogger; + logging?: ILogger | LogLevel; } diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHubConnectionOptions.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHubConnectionOptions.ts new file mode 100644 index 0000000000..7e41c9649e --- /dev/null +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/IHubConnectionOptions.ts @@ -0,0 +1,10 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +import { IHubProtocol } from "./IHubProtocol" +import { ILogger, LogLevel } from "./ILogger" + +export interface IHubConnectionOptions { + protocol?: IHubProtocol; + logging?: ILogger | LogLevel; +} diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Loggers.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Loggers.ts index 8d5202bbf3..3dd49d3ef5 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Loggers.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Loggers.ts @@ -18,3 +18,21 @@ export class ConsoleLogger implements ILogger { } } } + +export namespace LoggerFactory { + export function createLogger(logging?: ILogger | LogLevel) { + if (logging === undefined) { + return new ConsoleLogger(LogLevel.Information); + } + + if (logging === null) { + return new NullLogger(); + } + + if ((logging).log) { + return logging; + } + + return new ConsoleLogger(logging); + } +} \ No newline at end of file diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/connectionTests.js b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/connectionTests.js index c7102d3670..903538c49a 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/connectionTests.js +++ b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/connectionTests.js @@ -31,7 +31,7 @@ describe('connection', function () { var message = "Hello World!"; var connection = new signalR.HttpConnection(ECHOENDPOINT_URL, { transport: transportType, - logger: new signalR.ConsoleLogger(signalR.LogLevel.Information) + logger: signalR.LogLevel.Information }); var received = ""; diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js index 75ccc3795b..ba0d41387b 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js +++ b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js @@ -8,8 +8,7 @@ describe('hubConnection', function () { it('can invoke server method and receive result', function (done) { var message = "你好,世界!"; - var logger = new signalR.ConsoleLogger(signalR.LogLevel.Information); - var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType, logger: logger }), logger, protocol); + var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType }), { protocol: protocol}); hubConnection.onClosed = function (error) { expect(error).toBe(undefined); done(); @@ -30,8 +29,7 @@ describe('hubConnection', function () { }); it('can stream server method and receive result', function (done) { - var logger = new signalR.ConsoleLogger(signalR.LogLevel.Information); - var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType, logger: logger }), logger, protocol); + var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType }), { protocol: protocol}); hubConnection.onClosed = function (error) { expect(error).toBe(undefined); @@ -61,9 +59,7 @@ describe('hubConnection', function () { it('rethrows an exception from the server when invoking', function (done) { var errorMessage = "An error occurred."; - - var logger = new signalR.ConsoleLogger(signalR.LogLevel.Information); - var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType, logger: logger }), logger, protocol); + var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType }), { protocol: protocol}); hubConnection.start().then(function () { hubConnection.invoke('ThrowException', errorMessage).then(function () { @@ -85,8 +81,7 @@ describe('hubConnection', function () { it('rethrows an exception from the server when streaming', function (done) { var errorMessage = "An error occurred."; - var logger = new signalR.ConsoleLogger(signalR.LogLevel.Information); - var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType, logger: logger }), logger, protocol); + var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType }), { protocol: protocol}); hubConnection.start().then(function () { hubConnection.stream('ThrowException', errorMessage).subscribe({ @@ -108,8 +103,7 @@ describe('hubConnection', function () { }); it('can receive server calls', function (done) { - var logger = new signalR.ConsoleLogger(signalR.LogLevel.Information); - var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType, logger: logger }), logger, protocol); + var hubConnection = new signalR.HubConnection(new signalR.HttpConnection(TESTHUBENDPOINT_URL, { transport: transportType }), { protocol: protocol}); var message = "你好 SignalR!"; @@ -137,8 +131,7 @@ describe('hubConnection', function () { ServerSentEvents: "Error occurred" }; - var logger = new signalR.ConsoleLogger(signalR.LogLevel.Information); - var hubConnection = new signalR.HubConnection(new signalR.HttpConnection('http://' + document.location.host + '/uncreatable', { transport: transportType, logger: logger }), logger, protocol); + var hubConnection = new signalR.HubConnection(new signalR.HttpConnection('http://' + document.location.host + '/uncreatable', { transport: transportType }), { protocol: protocol}); hubConnection.onClosed = function (error) { expect(error.message).toMatch(errorRegex[signalR.TransportType[transportType]]);