@microsoft/signalr - log stuff in development stage only

Viewed 79

I'm working on an ASP.NET Core 6 & Angular 12 (Nx) project. You can take a look at an example angular Nx project.

The question is how do I enable the logging .configureLogging(LogLevel.Debug) for development only and disable it in production? In other words, if (!environment.production) { configureLogging... } wouldn't work here, because that service is located at a library folder.

~\frontend\libs\portfolio\src\lib\portfolio.service.ts

import { Injectable } from '@angular/core';
import {
  HubConnection,
  HubConnectionBuilder,
  LogLevel,
} from '@microsoft/signalr';
import { BehaviorSubject } from 'rxjs';

@Injectable({
  providedIn: 'root',
})
export class PortfolioService {
  connection!: HubConnection;
  message$ = new BehaviorSubject<string | null>(null);

  public connect(url: string): Promise<void> {
    return new Promise((resolve, reject) => {
      this.connection = new HubConnectionBuilder()
        .withUrl(url)
        .configureLogging(LogLevel.Debug)
        .withAutomaticReconnect()
        .build();

      this.setSignalrClientMethods();

      this.connection
        .start()
        .then(() => {
          console.log(
            `SignalR connection success! connectionId: ${this.connection.connectionId} `
          );
          resolve();
        })
        .catch((error) => {
          console.log(`SignalR connection error: ${error}`);
          reject();
        });
    });
  }

  private setSignalrClientMethods(): void {
    this.connection.on('SendAsync', (message: string) => {
      this.message$.next(message);
    });
  }
}

0 Answers
Related