Changing the way users can enable logging

This commit is contained in:
Pawel Kadluczka 2017-08-29 16:28:44 -07:00 committed by Pawel Kadluczka
parent b8d2d24b67
commit 7db480b23e
9 changed files with 89 additions and 32 deletions

View File

@ -16,7 +16,8 @@ describe("Connection", () => {
get(url: string): Promise<string> {
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<string> {
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<string> {
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<string> {
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);

View File

@ -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);
});
});

View File

@ -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;
}

View File

@ -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);
};

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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 ((<ILogger>logging).log) {
return <ILogger>logging;
}
return new ConsoleLogger(<LogLevel>logging);
}
}

View File

@ -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 = "";

View File

@ -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]]);